-2

How I can execute a jar file via java code?

I want to execute a java executable called "Console.jar" by code but I don't know how to do it.

Frank Soll
  • 420
  • 1
  • 4
  • 15
  • 1
    Open it for what purpose? A jar is more or less just a zip file. Use a zip utility. – Savior Apr 21 '16 at 15:43
  • 3
    Possible duplicate of [How to write a Java program which can extract a JAR file and store its data in specified directory (location)?](http://stackoverflow.com/questions/1529611/how-to-write-a-java-program-which-can-extract-a-jar-file-and-store-its-data-in-s) – Tobias Brösamle Apr 21 '16 at 15:47
  • @Pillar I want to open the file with Java. I don't want to extract it – Frank Soll Apr 21 '16 at 15:47
  • Then use Java's zip utility. But, again, what are you planning to do with the contents? – Savior Apr 21 '16 at 15:47
  • The jar file is a my creation file, and I want to open it with an another jar file @Pillar – Frank Soll Apr 21 '16 at 15:48

2 Answers2

2

A jar is simply a zip with a different extension.

So if you like to see what is present inside a jar simply rename it to .zip and open with your preferred zip client (winzip, winrar, unzip, 7zip...)


If you need to launch an executable jar from command line:

java -jar <jarFileWithJarExtension> 

for example

java -jar Console.jar

If you need to launch it from a java application you can do that with the following code:

Runtime.getRuntime().exec("java -jar Console.jar");

If you need also to interact with Console.jar take a look at the class Process

Davide Lorenzo MARINO
  • 26,420
  • 4
  • 39
  • 56
1

The pro way to do it ProcessBuilder:

//Create a process builder
ProcessBuilder builder = new ProcessBuilder("java", "-jar","path/f.jar", "argument 1","argument 2");

//start it
Process process = builder.start();

So you can have control over the jar you have already opened.You can get it's output,close it,be notified if it crashed etc.

GOXR3PLUS
  • 6,877
  • 9
  • 44
  • 93