How to execute an external program - java

I'm trying to execute a java program from a python program :
subprocess.Popen(["java -mx256m -jar /sphinx4-1.0beta5/bin/HelloWorld.jar"], shell=True)
but it fails with this error:
Error: Unable to access jarfile /sphinx4-1.0beta5/bin/HelloWorld.jar
i need to be in an specific directory : /home/karen/sphinx4-1.0beta-src, to execute the command:"java -mx256m -jar /sphinx4-1.0beta5/bin/HelloWorld.jar"
But i don't know how to do this. I need that my python program execute it !

use cwd parameter
subprocess.Popen(["java -mx256m -jar ../sphinx4-1.0beta5/bin/HelloWorld.jar"], cwd=r'path', shell=True)
http://docs.python.org/2/library/subprocess.html
"If cwd is not None, the child’s current directory will be changed to cwd before it is executed. Note that this directory is not considered when searching the executable, so you can’t specify the program’s path relative to cwd."

Your problem is probably related to your path to the jar file. Your code should most likely call /home/Karen/sphynx4-1beta-src in your popen call. This isn't a solution that will work on a different system, unless the file is in the same absolute path though.

Related

How to find file path to a bash file when the folder is renamed or moved to a new drive?

I've made an executable jar file for a terminal game that can be opened by typing java -jar name.jar in the Terminal.
Then I made a .sh file inside the same folder as the jar file to open it by double-clicking the .sh. I asked how to do this here, where people told me to use the following code in the .sh.
#! /bin/bash
DIR=$(dirname "$0")
java -jar "$DIR/game.jar"
This worked for a while, but when I renamed the folder, I realised if I move the folder to a pen drive the whole thing stops working and I get this in the Terminal.
Error: Unable to access jarfile /Volumes/Hard
logout
Saving session...
...copying shared history...
...saving history...truncating history files...
...completed.
[Process completed]
So how to find the file path to the folder the .sh and the jar are in, regardless of where it is, what its name is and what drive it is on?
Also, I'm using MacOS Mojave 10.14.4 if that's of any importance.
The error looks like the path does contain spaces, like probably /Volumes/Hard Drive/Users/something. The solution is to quote the command substitution.
Tangentially, don't use upper case for your private variable names.
But of course, the variable isn't really necessary here, either.
#!/bin/sh
java -jar "$(dirname "$0")/game.jar"
Nothing in this script uses Bash syntax, so it's more portable (as well as often slightly faster) to use sh in the shebang. Perhaps see also Difference between sh and bash
You can store the full path of the working directory using the environement variable $PWD, like in this example (done in 5min, it is just to show you how it is works) :
#!/bin/bash
DIR=$PWD
gamePath='java -jar '$DIR'/game.jar'
echo $gamePath
Wherever I will execute this script, it will shows up the working directory even if I change the name of the parent. Let me show you :
You can see that $PWD environnment variable works great.
Now, I will change the directory name from TestFolder to TestFolderRenamed and execute the script again :
So, in your case, change your code as following :
#! /bin/bash
DIR=$PWD
java -jar "$DIR/game.jar"
It should works.

How does java locate a JAR file?

The JAR file locates at C:\Workbench\jars\antlr-4.4-complete.jar
The environment variables are:
CLASSPATH=.;C:\Workbench\jars\*
PATH=C:\Workbench\jars;...
I am trying with the following commands:
java -jar "C:\Workbench\jars\antlr-4.4-complete.jar" <-- OK
java -jar antlr-4.4-complete.jar <-- FAIL!
java org.antlr.v4.Tool <-- OK
I am totally confused about the failed one. I am expecting the PATH variable will be looped through to locate the jar file. But it seems not. Why?
My guess is, the implementation of java -jar command line doesn't use the PATH variable for searching jar file. But still, why?
The PATH is for looking up the command to execute, in this case java will be looked up on the PATH.
You will need to supply either an absolute or relative path to java -jar because the terminal (bash/windows/zsh/etc...) will not expand arguments in this way. CLASSPATH is used by Java to look up further jars, but it expects a correct path to the initial jar as the first argument.

How do I set $PATH using PHP?

My site is being hosted on a shared server so I don't have su access. I needed to run a piece of code with java but it's not available on the server. So I got a self-extracting version of java and put it on the server in my home directory. Then I gave executable permissions to java and I try running the code. I have to use relative paths when running the file because of the restrictions of the server.
Trying to run the java file ../java/bin/java -jar 'javafile.jar' gives me the following:
error while loading shared libraries: libjli.so: cannot open shared object file: No such file or directory
I looked and libjli.so is located at ../java/lib/i386/jli/libjli.so. So I'm thinking that because I'm running java using a relative path it doesn't exactly know how to look for the other files. I'm hoping that if I can add absolute/path/to/java/bin to $PATH then this issue will be resolved.
So once I'm running my PHP, I can use dirname(__FILE__) to get the full path of my java bin directory. I've tried the following code:
exec('export PATH='.$bin_path.':$PATH', $output, $return);
print_r(array(getenv('PATH'), $output, $return));
Prints:
Array(
[0] => /usr/local/admin/bin:/usr/local/admin/bin/servers:/sbin:/usr/sbin:/usr/local/sbin:/usr/local/bin:/bin:/usr/bin,
[1] => Array(),
[2] => 0
)
So nothing was added to $PATH, no output was given, and the command returned a successful exit value. Is it just the restriction of the server that is preventing me from getting this working?
Firstly, this is not going to work.
exec('export PATH='.$bin_path.':$PATH', $output, $return);
It will launch a child process with a shell, run the export command in the shell, and then the shell will exit. But the export command only changes $PATH for that shell.
I'm not sure, but I suspect that you need to use putenv.
I'm hoping that if I can add absolute/path/to/java/bin to $PATH then this issue will be resolved.
Well, it could only help if you used a simple command name for invoking the java command.
And it would be simpler to just run java using the full absolute pathname; e.g. "/absolute/path/to/java/bin/java"

Expand wildcard in program's arguments

I have batch file which attempts to launch a java application:
java -jar myProgram*.jar
I would like the batch file to evaluate the wildcard * in order to find the program regardless of version numbers. So it should find myProgram1.jar, or myProgram438.jar and run it.
But this batch file yields:
Error: Unable to access jarfile myProgram*.jar
It looks like the arguments to java are not being processed by the shell. Is there a way to expand the wildcard in the arguments before passing them? I know that Bash has backtics which could do this. Is it possible to do in windows?
If there are more program*.jar in the folder you have to specify, which you want to start. The script starts the "last found":
#echo off&setlocal
for %%i in (myProgram*.jar) do set "jarProg=%%~i"
java -jar %jarProg%
You don't need "Cygwin" for this.

How to get the path of the currently running batch job in MS-DOS?

I have a batch foo.bar in a directory d:\progs\.
In this directory I have a jar file called bar.jar.
I have added d:\progs\ to my Path environment variable.
Now i do cd c:\anotherdir.
In this new dir i do foo -v. But i get the following error :
Unable to access jarfile bar.jar
How can i get the current path of foo.bar since %CD% returns c:\anotherdir ?
The code snippet you're looking for is %~dp0. It gives you the path of the currently running batch job.
This also has the advantage of allowing you to run the command from any location, whereas capturing %CD% in the beginning of your batch script will capture the directory that you were in when you started the batch script. (If this is what you want to do, however, that would be the preferred solution, as suggested by Richard.)
I have added d:\progs\ to my Path environment variable.
That only affects launching programs, it doesn't help with arguments (eg. data files and documents) passed to the program.
How can i get the current path of foo.bar since %CD% returns c:\anotherdir ?
Use %CD% to capture the current folder, and thus build the data file's path before changing directory.

Categories

Resources