4

After converting my java program to executable jar file using commands in command prompt in windows 10,while executing jar file I am getting error:

Could find or load main class Combine.class" caused by:java.lang.ClassNotFoundException:Combine.class

My jdk-11.0.1 has javamail api and excelapi.While executing I have set my classpath as:

classpath=%classpath%;C:\Program Files\Java\jdk-11.0.1\javamail_api\javax.mail-1.6.2.jar;C:\Program Files\Java\jdk-11.0.1\javamail_api\activation.jar;C:\Program Files\Java\jdk-11.0.1\jexcelapi\jxl.jar;.;

It was compiling and executing properly but after converting to executable jar file it is not running and giving above error.

Any help would be appreciated. Thank you

samabcde
  • 6,988
  • 2
  • 25
  • 41

1 Answers1

1

The clue is in the exception message. It is trying to load a class with the name Combine.class. But the classes real name is Combine.

You have created the JAR file incorrectly.

 echo Main-Class: Combine.class > manifest.txt 
 jar cmf manifest.txt FinalExecutable.jar Combine.class 

If Combine is in the default package (i.e. it doesn't have a package statement) then the above should be:

 echo Main-Class: Combine > manifest.txt 
 jar cmf manifest.txt FinalExecutable.jar Combine.class 

If Combine is declared in package foo.bar, then the above should be.

 echo Main-Class: foo.bar.Combine > manifest.txt 
 jar cmf manifest.txt FinalExecutable.jar foo/bar/Combine.class

and you need to be in the directory above the foo directory.

NB: the "Main-Class" attribute in the manifest must be a Java fully qualified class name, NOT a filename or file pathname.

It also should be noted that the CLASSPATH environment variable and the -cp argument will be ignored when you run a JAR using java -jar .... If your executable JAR depends on other JAR files, you should either combine them (to create a shaded JAR) or you should add a "Class-Path" attribute to the manifest; see https://docs.oracle.com/javase/tutorial/deployment/jar/downman.html

Finally, my advice would be to use a build tool (e.g. Maven) to compile your code, create the executable JAR file, etc rather than doing it by hand.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216