3

I built a javaFX artifact using IntelliJ. My first problem was in the Application Class box, where it didn't automatically detect my main class, or even suggest any classes as main class when I searched in the search box. I saw that it was a bug with IntelliJ, somewhere on StackOverflow, and I just manually entered my packageName.Main in the application class box.

I have two dependencies that I extracted into this output root. The output of building the artifact looks like this.

Then after rebuilding the project the JAR runs on my PC 100%, even if I move the JAR to another directory, it works 100%.

So I copied this JAR (Swansong.jar) to my other PC and when I double-clicked it, nothing happended. When I ran it in the cmd: java -jar Swansong.jar I got this error:

Error: Could not find or load main class swansong.Main
Caused by: java.lang.ClassNotFoundException: swansong.Main

I read about manifest files but I can't find a manifest file anywhere in my project, but if I extract the JAR file the manifest file that was generated looks like this. Can anyone please help me, why does the JAR run on one PC but not on the other? And how can I fix this?

UPDATE: This is the contents of my jar file and this is the contents of the swansong directory inside the jar file.

UPDATE 2: I feel stupid. My main PC runs jdk8 and my other pc runs jdk11. And JavaFX is not included in the jdk11. So I think this is the problem, but not 100% sure yet. Will test and see. Are there any workarounds for this?

  • Try to run java `-jar swansong.Swansong.jar` from parent folder of `swansong` – Eugen Oct 23 '19 at 11:24
  • if there wasn't a manifest file in your jar, it wouldn't run on your first pc either. did you run it in your IDE first, and only now by clicking on it? – Stultuske Oct 23 '19 at 11:28
  • @Eugen I only copied the JAR file to the other PC, so the directory structure looks like this: /Downloads/Swansong.jar – Albert Hattingh Oct 23 '19 at 11:30
  • @Stultuske I ran it on my IDE, worked, then just by double-clicking jar file, also worked. Only when copied to other pc it didnt work. And I noticed there is a manifest file inside the jar, as you can see in my last screenshot – Albert Hattingh Oct 23 '19 at 11:32
  • @Stultuske there just wasnt a manifest file in my project structure (in the IDE), but there is one in the jar – Albert Hattingh Oct 23 '19 at 11:33
  • @AlbertHattingh it's clear there was one,if not, it wouldn't know the name of the main class. – Stultuske Oct 23 '19 at 11:33
  • @Stultuske I assume a manifest file was generated by IntelliJ when I created the artifact? – Albert Hattingh Oct 23 '19 at 11:37
  • The problem does not seem to be the manifest - it exists and it contains the main-class line, otherwise you wouldn't get the error about the class not being found, Java wouldn't know which class to load. This error would indicate that there is no actual class file for swansong.Main in the jar. – Gimby Oct 23 '19 at 11:45
  • @Gimby thanks for your comment. I also dont think the problem is the manifest, since it is in the jar, but my Main class is also in the jar. will update my question now with a screenshot – Albert Hattingh Oct 23 '19 at 11:47
  • could it be something to do with the classpath? – Albert Hattingh Oct 23 '19 at 11:52
  • I am more suspecting a hidden space in a directory name, I have seen that exact problem in another question not too long ago. – Gimby Oct 23 '19 at 12:23
  • Sanity check: what is the exact size of your .jar file, in bytes? Is it exactly the same size on the other PC? – VGR Oct 23 '19 at 13:46
  • @VGR it is exactly 3062KB on both PCs. I think it is to do with classpath, because my classpath is empty in the jar file and I have dependencies. [this] (https://stackoverflow.com/q/18093928/10570170) question seems like it is on the spot my problem, but I still dont know how to fix the problem – Albert Hattingh Oct 23 '19 at 18:30
  • I didn’t ask how many kilobytes each file is; I asked for the exact number of bytes. If you have the tools to do so, a simple hash like an MD5 hash would also be useful. My goal is to determine if the files are in fact identical. As for the classpath, I doubt that would matter, since the purpose of a “fat jar” is to include everything in a single archive. – VGR Oct 23 '19 at 18:55

2 Answers2

1

I hate to admit it, but the solution to the problem was staring me in the face all along, and I am truly embarrassed. I used Java 8 to develop the program. Target PC was using Java 11 and I never thought to check the JDK version, because I thought I was using JDK8 on both PCs.

JavaFX is included in JDK8, but not in JDK11. It should be added to a project as an external library when using newer JDK versions. I just used JDK8 for running the program and everything works 100%. Thanks for the answers and comments though, I learned a lot about manifests and classpaths trying to solve this problem, so the effort was not in vain!

-1

Any executable jar file needs to have manifest file which points to the main class.

-JAR File
 \---META-INF
   \--MANIFEST.MF

In the MANIFEST.MF, you should introduce the main class as this:

    Main-Class: com.mycompany.MainClass
    Class-Path: . 

com.mycompany.MainClass should be replaced with appropriate path (fully qualified name of your main class)

  • thanks for your answer. there is a manifest file in my jar file, that contains `Main-Class: swansong.Main` which is my fully qualified name. – Albert Hattingh Oct 23 '19 at 11:44
  • And where are the dependency jar files (the path)? Have you introduced them in the manifest? – Ahmadreza Sedighi Oct 23 '19 at 11:50
  • I have not introduced them in the manifest. The screenshot of my manifest is my entire manifest, but I referenced Jsoup and Jfoenix in my source code and added it to the output layout. Nothing in the manifest though. – Albert Hattingh Oct 23 '19 at 11:56
  • No in my manifest the class-path is empty, and i don't know what it should be – Albert Hattingh Oct 23 '19 at 12:18
  • https://dzone.com/articles/java-8-how-to-create-executable-fatjar-without-ide – Ahmadreza Sedighi Oct 23 '19 at 12:25
  • `Class-Path: .` is wrong. The value of class-path must be a space-separated list of relative URLs. Directory entries must end with `/` and they should contain resources, not other .jar files. See [the Jar File Specification](https://docs.oracle.com/en/java/javase/13/docs/specs/jar/jar.html#class-path-attribute). – VGR Oct 23 '19 at 13:44