• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Self-Running JAR

 
Ranch Hand
Posts: 64
4
Eclipse IDE C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It seems that JARs, when executed by double-clicking, run with default options. How can I make it so that an executable JAR, upon being openned, run itself with some JVM options without the user ever needing to touch a console or terminal? Perhaps the heap size should be bigger or I want it to call itself with an extra classpath or something.



where foo.bar.Main is the "real" main class.

P.S. I've done something like this before for a GUI application, and even had a friend help me test it out. For some reason, this worked before, but not anymore. No GUI shows up.
 
Rancher
Posts: 5008
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

when executed by double-clicking,


A solution I've used on Windows is to modify the commandline in the Registry to include the desired options.
 
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Go through the Java™ Tutorials. It tells you how to add a CLASSPATH to a .jar. I can't remember whether it says anything about heap size.
 
Louis Denning
Ranch Hand
Posts: 64
4
Eclipse IDE C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I want to be able to have the JAR re-execute itself with JVM arguments determined at runtime, whatever the options may be. A bad example would be if I wanted the JAR to read some config files or something, determine what JVM arguments to use, and then re-execute itself with those arguments using ProcessBuilder or Runtime.exec(String) or something (bad example because the user might as well have ran a shell or batch script or something with the options specified).

Dumb deployer:

Dumb main:


I find it really bizarre that what I did earlier used to work.
 
Norm Radder
Rancher
Posts: 5008
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You've posted 2 classes but haven't explained how they fit with the problem.
Where is the jar file in this?  What files are in the jar file?
What happens when you execute the code now?  
If there are problems, how are you trying to debug the program?
 
Louis Denning
Ranch Hand
Posts: 64
4
Eclipse IDE C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The test JAR contains only those two classes, where the manifest specifies JARMain as the main class. Upon running the JAR, once by double clicking the JAR, and once by command line, no GUI shows up, as mentioned in the first post. Running the JAR with Main as the main class works, but, of course, that does not accomplish my goal.
 
Norm Radder
Rancher
Posts: 5008
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What happens when you execute the java -jar TheJarFile.jar command in a command prompt?

Other debugging ideas:  Save the created Process and get its output and error streams and display them.
 
Louis Denning
Ranch Hand
Posts: 64
4
Eclipse IDE C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Error: Could not find or load main class Main

Nothing else from stderr. Very bizarre since this used to work just fine. Perhaps I missed a detail?
 
Norm Radder
Rancher
Posts: 5008
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Error: Could not find or load main class Main


Check the contents of the jar file.  Is the Main.class file there?

You can open a jar file with any program that can open and view a zip file.
 
Louis Denning
Ranch Hand
Posts: 64
4
Eclipse IDE C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, both classes are where they should be.
 
Norm Radder
Rancher
Posts: 5008
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is the value of the classpath used with the -cp option?  Assign it to a String and print its value before it is used.
 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also go back to the tutorial link I posted and compare the contents of your Manifest file with the example given there. Is the name of the classes exactly the same as in the Manifest file? You must include any package names
mypackage.MyClass
And end the Manifest file with an empty line.
 
Norm Radder
Rancher
Posts: 5008
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The Main class is referenced from the ProcessBuilder call in the JARMain class.  That would mean that the main() method in the JARMain class was successfully executed.  Which would mean the manifest file pointed to the JARMain class.

My test of the code works.  When I change the name of the class referenced in the ProcessBuilder call, I get this error:
Error: Could not find or load main class JARMainTestXXX
 
Louis Denning
Ranch Hand
Posts: 64
4
Eclipse IDE C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I seem to have discovered what the issue was. There was a URL-style "%20" where a space should've been in the classpath returned by JARMain.class.getProtectionDomain().getCodeSource().getLocation().getPath(). Simply adding .replace("%20", " ") seems to have solved the problem.
 
Norm Radder
Rancher
Posts: 5008
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Was the %20 printed in the classpath?  Could you post that output so we could see what it looked like?
 
Louis Denning
Ranch Hand
Posts: 64
4
Eclipse IDE C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Norm Radder
Rancher
Posts: 5008
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks.
 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What a strange thing to happen. But well done persevering and finding the problem and showing us it.
 
Louis Denning
Ranch Hand
Posts: 64
4
Eclipse IDE C++ Java
  • Likes 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
After a little more research, it seems that the reason why "%20" appears in place of a " " is due to the behaviour of the URL class. Simply converting the URL into a URI before calling getPath() on it solves the issue. This may help catch other URL-style percent codes that may arise from Unicode characters in the classpath or something.

Thanks for the help!
reply
    Bookmark Topic Watch Topic
  • New Topic