3

Im trying to access server data via a jar-file. Doing this in MATLAB is quite simple:

javaaddpath('*PATH*\filename.jar')
WWS=gov.usgs.winston.server.WWSClient(ip,port);
Data = eval('WWS.getRawData(var1,var2,var3)');
WWS.close;

Problem is that I need to execute this in Python and I can't figure out how to translate these few lines of code. I've tried using the subprocess module like:

WWS=subprocess.call(['java', 'gov/usgs/winston/server/WWSClient.class'])

but the best I can get is the error "could not find or load main class gov.usgs.winston.server.WWSClient.class"

Thankful for all the help!

2
  • Your address is to a class (in your python example)! But you wanna run a jar file. Right? Commented Nov 23, 2020 at 20:30
  • Yes that is correct, the .jar contains hundreds of classes only and the one that i need execute first is WWSclient.class Commented Nov 24, 2020 at 5:57

2 Answers 2

2

Also you can use the following code:

import subprocess

command = "java -jar <*PATH*\filename.jar>"
result = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate()

And result is the output of the jar file.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for your help but it does not seem to go through either unfortunately: I am receving this error on Mac (b'', b"/bin/sh: -c: line 0: syntax error near unexpected token newline'\n/bin/sh: -c: line 0: java -jar <usgs.jar>'\n") and on Windows it says The syntax of the command is incorrect.
To clarify, I want to execute a specific class (WWSClient.class) within the jar file with the two variables "ip" and "port". Is It possible to execute WWSClient.class without opening the jar?
2

There are a few ways you can do this. One of the easiest ways is

import subprocess
subprocess.run(["java", "-jar", "*PATH*\filename.jar"])

The python subprocess command runs a system command. It takes a list as an argument, and the list is just the system command you want to run and it's arguments.

3 Comments

Thanks so much for you reply, I'm receiving the error no main manifest attribute, in *path*/filename.jar CompletedProcess(args=['java', '-jar', '*path*/filename.jar'], returncode=1)
The main manifest attribute is added to the jar file during its creation. Did you make the jar file itself, and if so, did you use an IDE? Most IDE's have a way of creating the Manifest so you don't have to create one yourself. If you're using Intellij, this link might help.
I didn't do it myself and unfortunately I don't know Java that well so I understand fully what it is doing. It basically opens up to my server and gather the requested data for me, if I supply the correct class (WWSclient.class) with the information on ip and port.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.