Within a Java 1.8 program, how can I execute a program on the user's PATH? For simplicity let's assume that only *nix systems have to be supported. What I want to do is something like:
Runtime.getRuntime().exec("myProgram")
where myProgram is somewhere on the user's PATH.
Runtime.getRuntime().exec(cmd) can only "see" programs on the system's path. For example in my case (mac os) /usr/bin:/bin:/usr/sbin:/sbin, but what if myProgram is in /usr/local/bin or ~/bin?
I tried to get the directories on the user's PATH by parsing the output of Runtime.getRuntime().exec("echo $PATH"), but I get back only the string "$PATH" itself, not the actual content of the PATH variable.
EDIT Executing System.getenv() gives me only PATH=/usr/bin:/bin:/usr/sbin:/sbin, i.e. not the user's PATH.
If you want to get the actual content of the PATH variable you can use:
System.getenv():
Good luck!
String userPath = System.getenv("PATH");
If it returns a null string then try exporting that $PATH variable before running your java program.
Related
Whenever I try and run mycommand.exe from my Windows cmd.exe terminal, I get this error:
''mycommand.exe' is not recognized as an internal or external command, operable program or batch file'
Secondly
I've also experienced a similar error when I tried to run C:\Program Files\My-App\Mobile.exe
''C:\Program' is not recognized as an internal or external command, operable program or batch file'
This is a very common question seen on Stackoverflow.
The important part here is not the command displayed in the error, but what the actual error tells you instead.
a Quick breakdown on why this error is received.
cmd.exe Being a terminal window relies on input and system Environment variables, in order to perform what you request it to do. it does NOT know the location of everything and it also does not know when to distinguish between commands or executable names which are separated by whitespace like space and tab or commands with whitespace as switch variables.
How do I fix this:
When Actual Command/executable fails
First we make sure, is the executable actually installed? If yes, continue with the rest, if not, install it first.
If you have any executable which you are attempting to run from cmd.exe then you need to tell cmd.exe where this file is located. There are 2 ways of doing this.
specify the full path to the file.
"C:\My_Files\mycommand.exe"
Add the location of the file to your environment Variables.
Goto:
------> Control Panel-> System-> Advanced System Settings->Environment Variables
In the System Variables Window, locate path and select edit
Now simply add your path to the end of the string, seperated by a semicolon ; as:
;C:\My_Files\
Save the changes and exit. You need to make sure that ANY cmd.exe windows you had open are then closed and re-opened to allow it to re-import the environment variables.
Now you should be able to run mycommand.exe from any path, within cmd.exe as the environment is aware of the path to it.
When C:\Program or Similar fails
This is a very simple error. Each string after a white space is seen as a different command in cmd.exe terminal, you simply have to enclose the entire path in double quotes in order for cmd.exe to see it as a single string, and not separate commands.
So to execute C:\Program Files\My-App\Mobile.exe simply run as:
"C:\Program Files\My-App\Mobile.exe"
When you want to run an executable file from the Command prompt, (cmd.exe), or a batch file, it will:
Search the current working directory for the executable file.
Search all locations specified in the %PATH% environment variable for the executable file.
If the file isn't found in either of those options you will need to either:
Specify the location of your executable.
Change the working directory to that which holds the executable.
Add the location to %PATH% by apending it, (recommended only with extreme caution).
You can see which locations are specified in %PATH% from the Command prompt, Echo %Path%.
Because of your reported error we can assume that Mobile.exe is not in the current directory or in a location specified within the %Path% variable, so you need to use 1., 2. or 3..
Examples for 1.
C:\directory_path_without_spaces\My-App\Mobile.exe
or:
"C:\directory path with spaces\My-App\Mobile.exe"
Alternatively you may try:
Start C:\directory_path_without_spaces\My-App\Mobile.exe
or
Start "" "C:\directory path with spaces\My-App\Mobile.exe"
Where "" is an empty title, (you can optionally add a string between those doublequotes).
Examples for 2.
CD /D C:\directory_path_without_spaces\My-App
Mobile.exe
or
CD /D "C:\directory path with spaces\My-App"
Mobile.exe
You could also use the /D option with Start to change the working directory for the executable to be run by the start command
Start /D C:\directory_path_without_spaces\My-App Mobile.exe
or
Start "" /D "C:\directory path with spaces\My-App" Mobile.exe
when you have this problem
search for Environment variables
press on Environment variables
Under "System varibles" you search for PATH and press edit
press on new and add the path of your program and save it
After setting JAVA_HOME to C:\Program Files\Java\jdk1.8.0_171 and adding %JAVA_HOME%\bin to my path, I am still getting this error. I have checked that the path is correct multiple times and every solution online just says to add these two variables. Is there any other step to fix this?
I'm getting this error when trying to run "java -jar start.jar" for solr.
You have not added Java Location as the Path variable in your system. The Command Prompt or Powershell can only take you to the path/paths you have already specified.
Add "C:\Program Files\Java\jdk1.8.0_171\bin\" to the Path variable in your Advanced System Settings->Enviroment Variables->Path.
Make sure you run the command in a new DOS shell. If you are using the same one you ran the java command in before setting the path, it won't have the path update.
Don't type path manualy - choose path by [Browse directory] button.
Path looks the same but can be different (maybe encoding of signs)
I am trying to work out the semantics of using Java ProcessBuilder to call operating system processes and read this line out of the javadocs for the start command:
"This method checks that the command is a valid operating system command. Which commands are valid is system-dependent, but at the very least the command must be a non-empty list of non-null strings."
Tell me, What is considered a valid process for Mac and for Windows? Is it anything that can be found on the PATH variable?
Is it anything that can be found on the PATH variable?
Yes it is; although you can also specify the full path of the command if you like (such as "/bin/ls"). Another test if of course to check if the file in question is a regular file and has execution permissions.
Note: this will launch a "real" process, it will not launch it via a command interpreter; as such don't attempt to use pipes, file globs, shell builtins etc: those are interpreted by sh/cmd.
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.
I am trying to run a perl command with Java runtime exec in linux/ubuntu/gnome. The command generates an pdf file, but it saves it in my home folder. Is there any way that the exec method can set an output path for the commands executed? Thanks in advance.
The exec method just runs the command on the operating system, so you'll want to change the command you're running with exec more than anything in "Java" per se.
There are a few possibilities:
Change the working directory of your java program. The .pdf is being saved in your working directory because this is where the program is being run.
Unfortunately it's not simple to change this value after the program has been launched. It is, however, trivial to change before the program starts; just change the working directory before starting the program.
Move the file to it's desired location after it's been created in your home directory.
Change the command so that it includes the target location. Your perl script may have an option that will enable you to save it's output to a certain location (usually -o or --output). Using this your program would change from:
Runtime.exec("perl someprogram");
to something like:
Runtime.exec("perl someprogram -o /path/to/some.file")
You might be able to use "output redirection", if there is no option to do this.
Try something like what's below as your argument:
Runtime.exec("perl someprogram > /path/to/some.file")
Unfortunately, without knowing more details of your situation I can't provide more concrete advice.
While each approach has benefits and drawbacks, it's probably best to just implement the one that you understand best; if you can't get one to work, try another.
A good, free online resource for learning is Introduction to Linux: A Hands On Guide.
Section 2.2 has details on cd which you can use for 1..
Section 3.3, section 3 teaches about the mv command, which will be useful in 2..
Section 5.1 is about I/O redirection. Knowing about "output redirection" and the > operator, are important for 4..
For 3., you'll have to consult the documentation of the perl program you're using.
You could modify the Perl script to accept an absolute path for the output.
You can trying setting the working directory using exec(java.lang.String[], java.lang.String[], java.io.File) where File is the directory the command is executed from.
If all else fails, you'll can always copy the generated file from the Home directory to your final location.