0

I am using a jar to run some code as a helper for a program (OSX). I want to open this jar programmatically, and have been using a ProcessBuilder to run it through the terminal.

However, I want to give the jar some arguments (specifically a file location, but that's irrelevant). I have using java -jar jarName arg, but this doesn't work with people who don't have Java tools installed.

I have tried to use open jarName --args arg, but the jar doesn't recognize the args. As a test, I am just using the following code for now.

public static void main(String[] args) {
   try {
      // set a PrintStream to see the args presented
      System.setOut(
        new PrintStream(new FileOutputStream(new File(System.getProperty("user.home") + "/Desktop/argsTest.txt"))));
   } catch (FileNotFoundException e) {
      e.printStackTrace();
   }
   System.out.println(argsSize: "+args.length");

   for (String s : args)
      System.out.println(s);
}

I am fine with trying other methods of opening jars, so long as they are available on all up to date systems.

I have the JRE packaged in the application, is there a way to use that?

  • 2
    You need Java installed to run a jar – OneCricketeer Dec 03 '18 at 15:16
  • JDKs for Java 9 and later include the `jmod` and `jlink` tools which let you create a self-contained tree (called an “image”) which has an executable file which runs the program. A jlink’d image does not require the user to have Java installed. – VGR Dec 03 '18 at 17:23

2 Answers2

1

Bundle jre with with your program. Refer to Bundle JRE along with executable jar

or if you are using netbeans - it will allow you to test it first. https://netbeans.org/kb/docs/java/native_pkg.html

how to run - https://netbeans.org/kb/docs/java/native_pkg.html#check

Sheetal Mohan Sharma
  • 2,908
  • 1
  • 23
  • 24
0

Welcome on StackOverFlow!

Sadly, the answer is no, you can't do this. If you want to run some java code, from a jar or not, you will need to have java installed on the machine. Specifically, you need the JRE to have the JVM.

But you can provide java by your own when deploying your app. See this threads:

for more informations.

Kapcash
  • 6,377
  • 2
  • 18
  • 40
  • I have a JRE packaged in the application... is there a way to run the jar using that? – Thomas Varano Dec 03 '18 at 15:30
  • If you really have a jre packaged, you should be able to use the `java` command line tool. Just look at the installation folder of the jre, you should find the file `bin/java.exe`. Be careful to set the PATH variable so that your program find the java binary! – Kapcash Dec 03 '18 at 16:25