Skip to content
Advertisement

b’Error could not find or load main class caused by java.lang.classnotfoundation error’

I am trying to read the executable jar file using python. That jar file doesn’t have any java files. It contains only class and JSON files.

So what I tried is

from subprocess import Popen,PIPE

jar_location = C:\usersapp\my_file.jar"
inputs = C:\users\appmy_input.json"

my_data = Popen(["java", "-cp",jar_location,"SOMECLASS.class",inputs])
stdout,stderr = my_data.communicate()
print(stdout,stderr)

What my expected output is, it reads the input(inputs) and passes it to the given class method(someclass.class) and it should return some response for me.

But the actual output I am getting is a Class file not found error.

The class file is inside the jar file only

I tried with Popen([“java”,””-jar”,jar_location,inputs]), I am getting no main manifest attribute found. I can see a manifest file with the basic version.

Can someone help me how to read these class files through python? I have to do the same thing for multiple class files which are inside the jar file

Advertisement

Answer

You should not specify a filename inside a JAR file, but a class name. Try with

my_data = Popen(["java", "-cp", jar_location, "SOMECLASS",inputs])

(omit the “.class” extension please)

You find the documentation of the java tool (that you use to execute your jar-file) as The java tool in the collection of the JDK Tool Specifications

The synopsis variant you are using is java [options] mainclass [args ...], you gave the option for the classpath -cp <classpath> and you use the JAR-file as only element in the classpath.

Please note that if the JAR-file contains a MANIFEST.MF file, when you open it with a ZIP-tool, you should inspect that because it could contain a longer classpath that you might have to specify also on commandline, if you don’t use the -jar parameter. A manifest doesn’t always contain a main-class that is used for the execution via -jar, but still can contain other important parameters. And the values are always trimmed to a short line-length inside that file, don’t get confused by that.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement