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?
5 Answers
The Jar files run on any OS for which a JVM exists.

- 53,009
- 9
- 91
- 143

- 7,266
- 2
- 32
- 42
-
2Well, on any O/S for which the required version of the JVM is installed; if you use Java 6 APIs a Java 5 JVM does you no good. – Lawrence Dol May 19 '10 at 02:26
Yes, as long as you don't use any native libraries (JNI) this is how java works. It's platform independent.

- 39,095
- 19
- 120
- 139
-
And no Java Native Access :-) and keep in mind that the filepath separator is different on each platform. – Pindatjuh May 18 '10 at 22:52
-
@Pindatjuh: JNA is a layer on top of JNI so the native support limitation is a given; however it has support for multiple O/S's and, as it happens, JNA support the 3 he specifically mentions: `JNA has been built and tested on OSX (ppc, x86, x86_64), linux (x86, amd64), FreeBSD/OpenBSD (x86, amd64), Solaris (x86, amd64, sparc, sparcv9) and Windows (x86, amd64). It has also been built for windows/mobile and Linux/ppc64, although those platforms are not included in the distribution.` – Lawrence Dol May 19 '10 at 02:30
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

- 5,488
- 2
- 29
- 36
-
Double-clicking directly on a JAR rarely work, because it means that no arguments are sent to main or to the JVM. – Rhangaun May 18 '10 at 22:56
-
@Skeptic: Define "rarely". In practice I use lots of Java programs an none of them require command line arguments. – Lawrence Dol May 19 '10 at 02:31
-
@LawrenceDol I just created an executable jar file and on the machine where I actually wrote the code in it works perfectly fine ( I worked on a windows 8.1 pro) but when I tried to run the jar file from a windows 7 professional machine it gave me the JVM error and I wasn't sure how to fix it. What would you suggest? – Scarl Oct 13 '14 at 17:02
-
@Sara: Impossible to say without seeing your command line and the error; you could open a question and put the link in a comment here so I can find it. But my best guess is that the Windows 7 machine can't find the JVM executable and your command line is incorrect on that machine. I always install my JVM to a fixed location and use the explicit path, e.g. `C:\Program Files (x86)\JavaRE\bin\java -jar xxx.jar` – Lawrence Dol Oct 13 '14 at 19:16
-
@LawrenceDol http://stackoverflow.com/questions/26352227/running-a-jar-file-on-other-windows-versions – Scarl Oct 14 '14 at 03:36
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.

- 25,531
- 3
- 32
- 40
-
I second that, it's the case for my jar file because it was written to deal with Windows registry so it would make no sense to run it on a MAC OSX for that matter. – Scarl Oct 13 '14 at 17:03
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.