Unable to access jarfile using subprocess - java

I am trying to run a Java command in python like this:
import subprocess
subprocess.Popen(['java -Xmx1024m -jar /maui-standalone-1.1-SNAPSHOT.jar run /data/models/term_assignment_model -v /data/vocabulary/nyt_descriptors.rdf.gz -f skos'], cwd=r'/Users/username/Repositories/RAKE-tutorial/', shell=True)
Unfortunately it is throwing an Unable to access jarfile /maui-standalone-1.1-SNAPSHOT.jar error. I have checked the permissions and tried a number of other options including using the os.system command to run a shell script. The error still remains. There seem to be a lot of people who have encountered the same problem but none of their solutions seem to work for me. Any suggestions? Let me know if you need any more information. Thanks in advance!

use fullpath to file
like
command = f"java -jar {os.path.dirname(__file__)}\\jar_name.jar"
process = subprocess.run(
command,
shell=True,
universal_newlines=True,
capture_output=True,
text=True,
)
it help's me

Related

How to run java command as root?

I get the infamous error "Could not find or load main class..." when I am trying to run java command as root.
I tried this solution without any success.
which java returns /usr/bin/java so I tried appending at ˜/.bashrc
export PATH=$PATH:/usr/bin/java
or
export PATH=$PATH:/usr/bin, again... without success.
ps: I want to run as root because I've created a simple server using sockets and I want to ping it with an android app. The problem is that I am getting permission denied.
Maybe this could help:
sudo java -cp $CLASSPATH com.blubber.Bla

I was able to compile my java code from PowerShell, but cannot run it

I was able to compile and run my java code from CMD, however when I try to run the same commands in PS, I am getting error messages. I have read and been told that CMD commands will work in PS, but the CMD commands are not working in PS
Here is the line that I am using to execute my program:
java -classpath .;stanford-corenlp-3.8.0.jar;stanford-corenlp-3.8.0-
javadoc.jar;stanford-corenlp-3.8.0-models.jar;stanford-corenlp-3.8.0-
models.jar Test.TestCoreNLP
I am running the command from the directory where my needed JAR files are located. The error message says...
The command stanford-corenlp-3.8.0-models.jar was not found, but does exist
in the current location. Windows PowerShell does not load commands from the
current If you trust this command, instead type: ".\stanford-corenlp-3.8.0-
models.jar".
Made the change and the code looks like this now.
java -classpath .\;stanford-corenlp-3.8.0.jar;stanford-corenlp-3.8.0-
javadoc.jar;stanford-corenlp-3.8.0-models.jar;stanford-corenlp-3.8.0-
models.jar Test.TestCoreNLP
Still getting the exact same error message. I have also tried going up a directory and no luck. I have looked all over StackOverflow and I have done my research.
Any help would be much appreciated.
Thanks.
Using .\ would work for one file, but since you have a number of files, you should reference the current directory in each one of those files.
java -classpath .\stanford-corenlp-3.8.0.jar;.\stanford-corenlp-3.8.0-javadoc.jar;.\stanford-corenlp-3.8.0-models.jar;.\stanford-corenlp-3.8.0-models.jar .\Test.TestCoreNLP
Java 6 also supports wildcards, as this answer indicates, so you might try simply this.
java -cp ".\*" .\Test.TestCoreNLP
I came here with similar trouble, and what I found is that when running like this:
java -cp .\target\somelib.jar;.\target\myapp-1.0-SNAPSHOT.jar com.ethoca.app.myapp
I would get help info. My discovery is that I need to double-quote my list of classpath, like:
java -cp ".\target\somelib.jar;.\target\myapp-1.0-SNAPSHOT.jar" com.ethoca.app.myapp

Mac launcher script for Java app

I need to launch a Jar file with a portable JRE. It works on Windows thanks to launch4j, it works on Ubuntu thanks to me, but not on Mac ..... and I can't test it.
The path tree of application:
abcmap/
launcher.command
bin/
abcmap.jar
jre/
bin/
java
Thinking it's like Unix system, I tried this (without specify bash):
./bin/jre/bin/java -Xms512m -Xmx1024m -jar ./bin/abcmap.jar
But this error appear:
MacBook-Pro:~ frcstnt$ /Applications/abcmap-mac/abcmap.command;
exit;
/Applications/abcmap-mac/abcmap.command: line 1:
bin/jre/bin/java: No such file or directory
logout
All files are rwx for all,
All files are UTF8 without BOM
I tried to change end of line to Unix and to "Old Mac" with Notepad++,
Archives are available on my website:
http://abc-map.fr/download/
https://translate.google.fr/translate?hl=fr&sl=fr&tl=en&u=http%3A%2F%2Fabc-map.fr%2F
Someone can help me please?
Edit: I think my problem is in the launcher, maybe encoding or bad command ?
Edit: Thanks to the Wim's answer it works. This is the script:
#!/bin/bash
SCRIPTDIR=$(dirname $0)
cd $SCRIPTDIR
${SCRIPTDIR}/bin/jre/bin/java -jar ${SCRIPTDIR}/bin/abcmap.jar
The path ./bin/jre/bin/java will be resolved relative to the current directory, not relative to the location of the command file. Try what happens if you run the command from the directory where the command is located.
I think I've found thanks to Wim Deblauwe. I use this:
#!/bin/bash
SCRIPTDIR=$(dirname $0)
cd $SCRIPTDIR
${SCRIPTDIR}/bin/jre/bin/java -jar ${SCRIPTDIR}/bin/abcmap.jar
Find the script dir, go to this dir and start operations.
It seems like you maybe don't even have the Java Runtime Evironment installed. You should try running java -version or at least java -h or java --help.
If you output is something like the following:
java: command not found
You should download and install the latest JRE and try again.
If, however, you see your java version, try first using cd to go to the directory of your jarfile and then use:
java -jar -otheroptionshere jarfile.jar
I hope this helps! Tell me, if you still got issues.

Java runs in CMD but not in Powershell

So, I am not new to Java and compiling in cmd but I am pretty new to using windows powershell. I have 'javac' and 'java' commands running fine in CMD and all java programs compile using netbeans and eclipse. I can also use javac inside windows powershell with no problems, but when attempting to run a java program, i.e. java MyProgram, after compiling it I get the following error...
Error: could not open 'C:\Program Files (x86)\Java\jre7\lib\jvm.cfg
Now I am running Java 8, so I have no idea why it would be looking for a file like this since CMD confirms that both 'java' and 'javac' are running version 8 and have the correct path set, or they would not run there. And please do not just tell me more about setting the paths because I have checked and checked and checked again. Also, I have made sure that anything that is related to java version 7 that I could find has been uninstalled / deleted.
Well, any advice would be appreciated. Thanks a bunch.
Try to start you application using a small cmd oder bat-File. Powershell does a lot of parsing and destroys your call. I usualy use something like this:
$Result = Invoke-Expression "cmd /c $Tempbat"
One alternative is to use the stop-parsing character: --%
icacls c:\scripts --% /grant ***\ScriptAdmin:(CI)(OI)F

Process not found when running from getRuntime.exec() from the JVM

I am trying to run the following code from within Eclipse:
Process process = Runtime.getRuntime().exec("gs");
However I get the exception:
java.io.IOException: Cannot run
program "gs": error=2, No such file or
directory
Running gs from the command prompt (OS X) works fine from any directory as it is on my PATH. It seems eclipse doesn't know about my path environment variable, even though I have gone into run configurations and selected PATH on the environment tab.
In additional effort to debug this issue I tried the following code:
Process process = Runtime.getRuntime().exec("echo $PATH");
InputStream fromStdout = process.getInputStream();
byte[] byteArray = IOUtils.toByteArray(fromStdout);
System.out.println(new String(byteArray));
The output was $PATH, hmm. Can someone nudge me in the correct direction?
you are assuming that exec() uses a shell to execute your commands (echo $PATH is a shell command); for the sake of simplicity you can use System.getenv() to see your $PATH:
System.out.println(System.getenv("PATH"));
EDIT
Often a better and flexible alternative to Runtime.exec() is the ProcessBuilder class.
I had the same issue and i found the problem. The Path Variable in Eclipse had different content than the one from the command Line.
Solution:
Look up for the $Path variable in command Line and copy the content. Then open Run Configuration->Environment and select new. Name: $PATH Value: insert the copied content.
That solved the Problem.

Categories

Resources