0

I am having an executable JAR. Ofcouser I have JDK installed at my end I am giving following command to run my exe JAR from command prompt.

1 Using JRE :-

C:\Users\userName\Desktop\Utility\latest>"C:\Program Files\Java\jre1.8.0_161\bin\java.exe" -jar Utility.jar

2 Using JDK

C:\Users\userName\Desktop\Utility\latest>"C:\Program Files\Java\jdk1.8.0_161\bin\javaw.exe" -jar Utility.jar

Both are working on my desktop but if I tries #1 to run the executable JAR on different machine which has only JRE Version (1.8 onwards) it is not getting opened up. I tried following links but some links are sayin to download few installers but all I do not want to get that. Is there any way. Or issue with my executable JAR ?

How can I make my executable JAR not need JDK to run Run a JAR file using a specific JRE

Manifest-Version: 1.0 Rsrc-Class-Path: ./ commons-collections4-4.3.jar poi-3.17.jar poi-ooxm l-3.17.jar xmlbeans-3.0.1.jar curvesapi-1.06.jar poi-ooxml-schemas-3. 17.jar poi-examples-3.17.jar poi-excelant-3.17.jar poi-scratchpad-3.1 7.jar commons-codec-1.10.jar commons-collections4-4.1.jar commons-log ging-1.2.jar curvesapi-1.04.jar junit-4.12.jar log4j-1.2.17.jar xmlbe ans-2.6.0.jar ooxml-schemas-1.3.jar Class-Path: ./ commons-collections4-4.3.jar poi-3.17.jar poi-ooxml-3.17.jar xmlbeans-3.0.1.jar curvesapi-1.06.jar poi-ooxml-schemas-3.17.jar poi-examples-3.17. jar poi-excelant-3.17.jar poi-scratchpad-3.17.jar commons-codec-1.10.jar commons-collections4-4.1.jar commons-logging-1.2.jar curvesapi-1.04.jar junit-4.12.jar log4j-1.2.17.jar xmlbeans-2.6.0.jar ooxml-schemas-1.3.jar Rsrc-Main-Class: DataProcessor.DataProcessor.App Main-Class: org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoade

Community
  • 1
  • 1
Pet
  • 41
  • 5
  • If java home is already set, no need to give full path. – Sambit Nov 19 '19 at 18:44
  • But even without JDK it is not working on other desktops – Pet Nov 19 '19 at 18:47
  • In other system, go to command prompt and type `java -jar utility.jar` and see any error. – Sambit Nov 19 '19 at 18:49
  • Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/log4j/Logger at DataProcessor.DataProcessor.App.(App.java:73) – Pet Nov 19 '19 at 19:12
  • As per above command use `-cp` option and give path of log4j jar file or copy the log4j file to the same location where utility jar file is there. – Sambit Nov 19 '19 at 19:17
  • my logger file is in the same path but even I give explicit path that exception remains same – Pet Nov 19 '19 at 20:05
  • My logger File is in the same path. But even I give explicit path then still exception remains same – Pet Nov 19 '19 at 20:06
  • Inside Utility.jar, unzip it and you will find manifest.mf file, please post the contents of that manifest.mf file. – Sambit Nov 19 '19 at 20:07
  • updated it above please have a look – Pet Nov 19 '19 at 20:49

1 Answers1

0

It sounds like the issue your are having is more than not having the JDK or knowing where the JRE on the target system is located, it's that you also didn't include the dependencies that your code has.

The jar file you have includes details in the manifest file that tells the JVM the classpath and the main class to load. If you look at the Rsrc-Class-Path, it is add the jars from the current directory. This is fine if you are sending the entire folder structure that includes all the jars in the expected location. But it doesn't work with just the jar.

In order to make a single jar that would run without any additional jars, you need to repackage the jars. There are two common ways to do this

  1. UberJar - where the class of your project is combined with the classes extracted form all of your dependencies into a single jar

  2. JarInJar - where your jar and all the dependecy jars are put into a jar and a custom classloader is used to load the classes from the jars inside the jar.

I'm not sure what build tool you're using, but for Maven the Shade Plugin will create an UberJar.

I personally recommend using the JarInJar option for this reason. The Spring Boot Maven Plugin is what I believe to the easiest

Jason Warner
  • 2,469
  • 1
  • 11
  • 15