0

Dear all (Sorry for the beginners question, it's my first post here..),

I am starting to do some Java coding with Visual Studio Code. My app runs fine in VSCode, and I can create a jar file. But I cannot run the jar file with "java PicSorter.jar", it says:

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

Any idea why? The build seems fine, I picked the main class and get in the terminal this:

*  Executing task: java (buildArtifact): default 

[OK] Successfully added the file to the exported jar: DBController$1.class
[OK] Successfully added the file to the exported jar: DBController.class
[OK] Successfully added the file to the exported jar: Gui.class
[OK] Successfully added the file to the exported jar: PicLabel.class
[OK] Successfully added the file to the exported jar: PicSorter.class
[OK] Successfully added the file to the exported jar: ShaHashOf.class
[OK] Successfully extracted the file to the exported jar: sqlite-jdbc-3.36.0.3.jar
[SUCCESS] Export Jar process is finished successfully *  Terminal will be reused by tasks, press any key to close it. 

(What) do I need to make it "executable" ? I was hoping that VSCode adds all this for me :-)

cp.
  • 1
  • 4
  • 1
    You appear to have created the JAR file incorrectly. It looks like you have set the Main-Class attribute to "PicSorter.java" when it should really be "PicSorter". The value should be the class name of the main class, not a source filename or compiled class filename. – Stephen C Jul 28 '22 at 14:31
  • Hmm, to create the jar I only clicked in VSCode on "Java Projects" -> right arrow symbol (Export jar..). Then a dialog appears asking for the main class where I choose "PicSorter" (it's a dropdown, the other choice is "without main class").. and then the output as shown is displayed in the terminal. – cp. Jul 28 '22 at 15:43
  • In the meantime I found, that I CAN run the same jar with "java -jar PicSorter.jar" - So it really looks I have to do some "black magic with the main class" to create a proper "executable" jar.. but what? :-) – cp. Jul 28 '22 at 15:54
  • Ah ... I missed that. Yes, the correct thing is the `-jar` option. You tricked me by saying that you were running `java PicSorter.jar`. To get that error message you must have actually run `java PicSorter.java` ... which is also wrong. There is no way to get `java xxx.jar` to work ... without the `-jar` option. – Stephen C Jul 28 '22 at 23:18
  • Thanks, I think I also tricked myself.. :-) – cp. Jul 29 '22 at 19:43

0 Answers0