开发者

Possible to run JAR file on any OS?

Is it possible to execute a JAR file on any OS (like Windows, Linux, Mac OS X)? I want to build a simple application开发者_运维百科 that I want to run on Linux, Windows, and Mac OS X. Could the JAR file be run on any OS with java installed?


The Jar files run on any OS for which a JVM exists.


Yes, as long as you don't use any native libraries (JNI) this is how java works. It's platform independent.


As other said, as long as you have Java installed and avoid using native code, you should be good to go. One thing to note is that you can usually run a JAR file just by double clicking it, and it opens like a native executable (on Windows this is how it works by default, on other OSes you can configure this behavior).

Such JAR files are called executable JAR files. If what you want to create is an executable JAR file, then you need to add a manifest file that tells the Java virtual machine (JVM) the name of the main class. Executable JAR files also can be run on the command line by doing:

java -jar myprogram.jar

If your JAR is not an executable JAR, then to run your program you have to add the JAR to your classpath and then execute the main class. To add a JAR to the classpath:

java -classpath path/to/your/program.jar com.mypackage.Main


Jar files are designed to run on any OS that has a JVM of a compatible version installed. Some jar files, however, may have be compiled from Java code that used OS-specific code (say talking to Windows registries), so testing it on other OS's is wise.


Yes, it can as long as it's not ruining from the terminal or command prompt (like java -jar name.jar.) it should work just fine.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜