Mohit Deshpande
Mohit Deshpande

Reputation: 55237

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?

Upvotes: 7

Views: 13481

Answers (5)

user7945825
user7945825

Reputation:

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.

Upvotes: 0

Cesar
Cesar

Reputation: 5488

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

Upvotes: 6

Snehal
Snehal

Reputation: 7496

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

Upvotes: 15

Kathy Van Stone
Kathy Van Stone

Reputation: 26271

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.

Upvotes: 5

tangens
tangens

Reputation: 39733

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

Upvotes: 13

Related Questions