0

I have a dummy jar, whose only job is to display a simple jdialog and ask user to select required memory size from the combo provided.After selecting required maximum memory user clicks start button to run my main Jar.

I am using runtime.getRuntime.exec() method in dummy jar to call my main jar.

I have hardcoded my main jars path its working fine.But I would like to know how do I get my Main jar's location programmatically when two jars are residing in different location??

I can get path of my dummy jar using approach used in below link.

How to get the path of a running JAR file?

But please advice me to get jar path of my main program..

Community
  • 1
  • 1
Kittu
  • 315
  • 1
  • 6
  • 14
  • Probably the easiest and most portable way is to have your installer create a properties file with all the locations. – biziclop Jul 18 '12 at 18:08

1 Answers1

1

You can't, unless you know where they will be installed/deployed. Why not package them both in the same JAR and then you can find the JAR path using the technique you described?

You should have two classes with main() methods, say org.kittu.Main and org.kittu.Dummy. Package them into main.jar and make sure org.kittu.Dummy is the class that gets executed by default (in the manifest).

In your Dummy class, get the location of main.jar and the amount of memory the user wants then exec() the something like:

java -Xmx <memory> -jar <path to main.jar> org.kittu.Main

to run your Main class.

Garrett Hall
  • 29,524
  • 10
  • 61
  • 76
  • Good idea, and you can use OneJar to package everything together. – biziclop Jul 18 '12 at 18:11
  • one jar???You want me to add dummy jar functionality also in my main project?? OK.If am doing in that way, after user's memory selection, are you suggesting me to call runtimr.exec() to execute the jar ? – Kittu Jul 18 '12 at 18:18
  • Yes, what would be the problem with that? – Garrett Hall Jul 18 '12 at 18:19
  • Problem would be again my main program comes to the same point asking user to select memory size.I can avoid this by setting some flag in my own property file during first execution isn't it??correct me if I am wrong. – Kittu Jul 18 '12 at 18:33
  • @Kittu That's right, you can pass a command line parameter, say `--run-main-program` or something similar. – biziclop Jul 18 '12 at 18:45
  • Please see updated answer, you can do everything from the command line arguments. – Garrett Hall Jul 18 '12 at 18:45