-1

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:\\users\app\\my_file.jar"
inputs = C:\\users\\app\my_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

vaanumalar
  • 57
  • 1
  • 11
  • 4
    There is no such thing as `java.lang.classnotfoundationerror`. You must transcribe error messages *accurately*, as well as *completely*. The error message names the class that wasn't found. It isn't on your CLASSPATH. So fix that. – user207421 Aug 12 '22 at 09:59
  • 1
    Of couse, you did a test from the command line first, and it worked? – Olivier Aug 12 '22 at 10:23
  • 1
    As an aside, you want to avoid `Popen` in favor of the higher-level wrapper functions. `result = subprocess.run(..., check=True, text=true, capture_output=True)` should do what you want in a single line of code (and get rid of the `b'...'` output as `bytes`, thanks to the `text=True` keyword argument). The lack of quotes around the strings in your question are also obvious syntax errors. For (much) more, see also https://stackoverflow.com/questions/4256107/running-bash-commands-in-python/51950538#51950538 – tripleee Aug 12 '22 at 10:40

1 Answers1

5

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.

cyberbrain
  • 3,433
  • 1
  • 12
  • 22