17

It is possible to run a JAR file locally. The next step is to run it on a different PC.

The question is whether the JRE, the JDK or both are required to run the JAR file?

030
  • 10,842
  • 12
  • 78
  • 123
Dhinakar
  • 4,061
  • 6
  • 36
  • 68
  • 1
    ofcource to RUN a Jar-File you only need the Java-RUNTIME-Environment (jre) and not the Java-DEVELOPMENT-Kit (jdk) in which the jre is included by the way. But i dont really know what you mean with `run this jar file into some other system`, do you simple mean to run in on a different pc? – nurgan Sep 04 '12 at 10:08

8 Answers8

40

The JDK contains the JRE.

Most program only need the JRE (Java Runtime Environment) but some programs need the Compiler at runtime in which case you need the JDK.

If you have the JDK, you don't need the JRE as well.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • @nobalG as I now have enough reputation to comment, here is a scenario for you to consider: I had a situation where I wanted to write code that compiles other code at runtime and then uses that compiled code. In my case I was creating a tool that could take a test class based on a particular framework, compile it, load the class, and extract test data from it so that the data could be used as part of an end-to-end test. In order for this tool to run properly it must be run with the JDK so that it can use the Java compiler. – D.B. Nov 16 '16 at 05:49
4

To run a jar file you only need java.exe(windows). JDK is the development kit for Java and JRE is the runtime. JDK contains JRE.

bugwheels94
  • 30,681
  • 3
  • 39
  • 60
Akhi
  • 2,242
  • 18
  • 24
  • 2
    For linux you can install java SDK using apt-get, or use synaptic or u can download the bin file from oracle site. – Akhi Sep 04 '12 at 10:20
  • The first statement is not true if the code being executed depends on items included only in the JDK, for example, `JavaCompiler`. See [this answer](http://stackoverflow.com/a/2946402/3284624) for an example. – D.B. Nov 16 '16 at 05:47
4

In the comments on the accepted answer nobalG asked, "Why the compiler is needed if jre is already there?"

At the time of writing I did not have enough reputation to comment, so I replied here instead.

I had a situation where I wanted to write code that compiles other code at runtime and then uses that compiled code. In my case I was creating a tool that could take a test class based on a particular framework, compile it, load the class, and extract test data from it so that the data could be used as part of an end-to-end test. In order for this tool to run properly it must be run with the JDK so that it can use the Java compiler.

D.B.
  • 4,523
  • 2
  • 19
  • 39
3

You only need JRE.

If the jar file you are trying to run has the Main-Class: <classname> header present in manifest file, then you can simply run the jar file by the command:

java -jar [your jar file name]

If the manifest file does not have that entry (and you know the fully qualified class name of the class containing main function), then you can run the jar file by the command:

java -cp [absolute path to jar file] [full qualified class name containing the main function]

Marko
  • 20,385
  • 13
  • 48
  • 64
  • This is not true if the code being executed depends on items included only in the JDK, for example, `JavaCompiler`. See [this answer](http://stackoverflow.com/a/2946402/3284624) for an example. – D.B. Nov 16 '16 at 05:44
3

To run a jar file you only need the JRE. You can run the jar file with the following command:

java -jar [jar file Name]

  • 1
    This is not always true. If the jar contains code that depends on items that are included with the JDK but not the JRE (e.g. `JavaCompiler`) then it may not run properly or at all without the JDK. See [this answer](http://stackoverflow.com/a/2946402/3284624) for an example. – D.B. Nov 16 '16 at 05:41
2

JRE is enough to run

JDK is used for development

Alexey
  • 7,127
  • 9
  • 57
  • 94
  • This is not true if the code being executed depends on items included only in the JDK, for example, `JavaCompiler`. See [this answer](http://stackoverflow.com/a/2946402/3284624) for an example. – D.B. Nov 16 '16 at 05:43
1

You need a JRE but not the JDK. The JRE is the java runtime environment and java code cannot be executed without it. The .jar is a compiled java file can and this needs the java runtime environment to be run.

KennyBartMan
  • 940
  • 9
  • 21
  • 1
    This answer is misleading because it implies that the JDK is not sufficient and not relevant. I believe a more precise answer would be: "You need one or the other. Either one is enough, because the JDK contains the JRE. Use JDK if you intend to do Java programming yourself." – Jon Coombs Mar 15 '14 at 04:03
  • This is not true if the code being executed depends on items included only in the JDK, for example, `JavaCompiler`. See [this answer](http://stackoverflow.com/a/2946402/3284624) for an example. – D.B. Nov 16 '16 at 05:45
0

You want to run the jar file; so you just need Java Runtime environment ( i.e. JRE).

rai.skumar
  • 10,309
  • 6
  • 39
  • 55
  • This is not true if the code being executed depends on items included only in the JDK, for example, `JavaCompiler`. See [this answer](http://stackoverflow.com/a/2946402/3284624) for an example. – D.B. Nov 16 '16 at 05:46