简体   繁体   中英

launching java applications in a cross platform way

I have a Java program, let's say it's called MyApp.jar

To run it on Linux, I just write a shell script called myapp.sh, which simply runs java -jar MyApp.jar and double click on it. Everything works well.

On Windows 7, I cannot assume that the java command will be in everyone's path (if that's what you call it on Windows), so I have to have a workaround and do something like create a batch script with this in it: "C:\\Program Files (x86)\\Java\\jre6\\bin\\java.exe" -jar MyApp.jar , which is obviously a Bad Solution... and it doesn't work on XP or Vista. It works on most Windows 7 machines though.

My application ships as a .zip file, the user extracts it to a folder MyApp, and then ideally will double click on something to invoke the program. So far it has a .sh script for Linux users to double click on, and a batch script for Windows 7 users (it works in most cases). It creates and consumes files in its own directory (MyApp).

What is the best way to invoke this program in a cross platform way?

Thanks!

As mentioned by biziclop, Java Web Start is commonly used to install/launch/update Java desktop apps.

As far as the JRE goes, it is usually sufficient to use the deployJava.js linked from that page above to ensure minimum necessary JRE is installed, and the elements in the JNLP file to specify any JRE micro-versions that are supported for the app.

Application launch

JWS can install desktop shortcuts and menu items when supported by the OS. Those can be specified in the launch file, or added/removed at run-time using services of the JNLP API.

Java application require a java runtime environment with a certain version to be installed on the target system.

The most convenient way, to my opinion, is to ship all libraries and documentation in a single archive and add start scripts for windows and *nix systems.

You could ship a JRE with your application too but then you'd have to support builds for different platforms...

If you can't deliver Java with your application, it might be a good solution to use JAVA_HOME to start it. It is set on, well... not all but many systems. Definitly you should not use C:\\Program Files (x86)... it is much better to use the environment variable %ProgramFiles%

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM