bError could not find or load main class caused by java.lang.classnotfoundation error

2024/9/20 8:45:41

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,PIPEjar_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

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.

https://en.xdnf.cn/q/119384.html

Related Q&A

Invalid value after matching string using regex [duplicate]

This question already has answers here:Incrementing a number at the end of string(2 answers)Closed 3 years ago.I am trying to match strings with an addition of 1 at the end of it and my code gives me t…

How to fetch specific data from same class div using Beautifulsoup

I have a link : https://www.cagematch.net/?id=2&nr=448&gimmick=Adam+Pearce In this link there data in divs with same class name. But I want to fetch specifi div. Like I want to fetch current g…

Python Matplotlib Box plot

This is my dataframe:{Parameter: {0: A, 1: A, 2: A, 3: A, 4: A, 5: A, 6: A, 7: A},Site: {0: S1,1: S2,2: S1,3: S2,4: S1,5: S2,6: S1,7: S2},Value: {0: 2.3399999999999999,1: 2.6699999999999999,2: 2.560000…

How to send turtle to random position?

I have been trying to use goto() to send turtles to a random position but I get an error when running the program.I am lost on how else to do this and not sure of other ways. My current code is:t1.shap…

How to scrape all p-tag and its corresponding h2-tag with selenium?

I want to get title and content of article: example web :https://facts.net/best-survival-movies/ I want to append all p in h2[tcontent-title]and the result expected is: title=[title1, title2, title3]co…

Tkinter: Window not showing image

I am new to GUI programming and recently started working with tKinter.My problem is that the program wont show my image, Im suspecing that it is my code that is wrong, however, I would like somone to e…

print dictionary minus two elements

Python 3.6All debug output is from PyCharm 2017.1.2I have a program that gets to this portion of the code:if len(errdict) == 21:for k, v in errdict.items():if k == packets output or bytes:continueprint…

Write CSV file using Python with the help of a csv dictionary / nested csv dictionary

I am having a csv file and i want to write it to another csv file. Its a bit complicated than it seems. Hoping someone to correct my code and rewrite it, so that i can get the desired csvfile. I am usi…

saving data to txt file using python

I am new in python, and I really need some help. I am doing this memory game where I need to save user, game score and time into a text file using python. I have tried several ways to do it, but nothin…

How can I create bounding boxes/contour around the outer object only - Python OpenCV

So Ive been trying to make bounding boxes around a couple of fruits that I made in paint. Im a total beginner to opencv so I watched a couple tutorials and the code that I typed made, makes contours ar…