1

For my java project on Eclipse, I added libraries to my project and the code runs fine (I open a web page with JCEF chromium and perform JSBridge).

I exported my project to a .jar and and when i want to execute my .jar with this command

"Path to java jdk11" -jar "Jar_Name".jar

I get this error

Exception in thread "main" java.lang.NoClassDefFoundError: org/cef/CefSettings

I don't understand why, indeed, when I start my project under Eclipse, it works.

I'm using Java 11, I exported as Jar files, Do you have any tips on how to make my .jar work on the command line?

Arnab
  • 4,216
  • 2
  • 28
  • 50
  • What command are you using to execute the JAR? What are the contents of the JAR? – Jeroen Steenbeeke Oct 18 '22 at 07:53
  • `NoClassDefFoundError` usually means the class was found when the project was compiled but it was not found when the JAR was executed. Please [edit] your question and post the entire stack trace. Also, does the JAR manifest file contain the `Class-Path` header? – Abra Oct 18 '22 at 07:55
  • I think you expect Jar_Name.jar to be a fat jar (meaning a jar that packages all the dependencies), but it's instead a simple jar (and nobody gives the dependencies at runtime). – Matteo NNZ Oct 18 '22 at 07:55
  • The problem is that Eclipse automatically adds the libraries to your class or module path. When you use `java -jar ...` you need to provide that yourself, e.g. by using`-cp` along with the path to the libraries. – Thomas Oct 18 '22 at 07:55
  • I use "Path to java jdk11" -jar "Jar_Name".jar and the contents are "bin", "src with Main.class", "lib" with all .jar I used for my project – jeremy jullienne Oct 18 '22 at 07:57
  • The library where the CerfSettings class is **jcef-78.1-win64.jar** and it is in my lib folder – jeremy jullienne Oct 18 '22 at 08:05
  • That's not the proper way to package a JAR. Class files should be placed directly in the JAR's root (inside their packages of course), and adding a lib directory does exactly nothing. If you want to include libraries in the JAR you need to place their contents in the archive root as well (this is what is known as a fat Jar, i.e. the thing that @MatteoNNZ referred to). Maven projects generally do this by using the maven-shade-plugin, but I'm not sure if you're using Maven – Jeroen Steenbeeke Oct 18 '22 at 08:06
  • 1
    @JeroenSteenbeeke You can specify relative paths to jar files in the Class-Path entry of the manifest. Using a `lib` directory relative to the main JAR file is relatively common. – Mark Rotteveel Oct 18 '22 at 08:35
  • What is the content of the manifest file (`META-INF/MANIFEST.MF`) of your JAR? Likely it doesn't have a `Class-Path` entry, or the relative paths are wrong. – Mark Rotteveel Oct 18 '22 at 08:38
  • 1
    @MarkRotteveel you'd think that after twenty years of using Java I'd have encountered that at least once Glad to have learned something new today, thanks – Jeroen Steenbeeke Oct 18 '22 at 08:46
  • @JeroenSteenbeeke To be honest, I find the (very) common practice to create fat-jars horrible. I wonder if people use it because they don't know about the alternative, or because they simply won't one file that does all. – Mark Rotteveel Oct 18 '22 at 08:51
  • @MarkRotteveel I don't know, I mostly work with WARs and EARs, and those have builtin mechanisms for libraries anyway – Jeroen Steenbeeke Oct 18 '22 at 09:06
  • @MarkRotteveel it depends on how "fat" your jar is. Often your dependencies come from the platform where you run and in that case, you will have them at runtime. But sometimes some deps are only for you andyou don't want to depend on them changing. Fat jars shouldn't be a quick way to have your jar work, but a responsible way to segregate dependencies that should be provided by dependencies that you do not want to be provided to avoid future compatibility issues. P.s. Spring is based on fat jars, and is far from being an example of bad practice :) – Matteo NNZ Oct 18 '22 at 16:46

0 Answers0