2

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!

  • Your address is to a class (in your python example)! But you wanna run a `jar` file. Right? – OmG Nov 23 '20 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 – Alexander Hebbe Nov 24 '20 at 05:57

2 Answers2

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.

desertnaut
  • 57,590
  • 26
  • 140
  • 166
  • 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) – Alexander Hebbe Nov 24 '20 at 06:55
  • 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](https://stackoverflow.com/questions/1082580/how-to-build-jars-from-intellij-properly/45303637#45303637) might help. – Conor Dunne Nov 24 '20 at 14:00
  • 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. – Alexander Hebbe Nov 24 '20 at 14:34
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.

OmG
  • 18,337
  • 10
  • 57
  • 90
  • 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 '\n") and on Windows it says The syntax of the command is incorrect. – Alexander Hebbe Nov 24 '20 at 07:13
  • 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? – Alexander Hebbe Nov 24 '20 at 07:14