This question already has answers here:
How to invoke a Linux shell command from Java
(3 answers)
Closed 6 years ago.
I'm using RedHat Linux bash command prompt. My requirement is to run a command using following code snippet where command could be anything which is allowd on command prompt eg: javac etc.
Process p = Runtime.getRuntime().exec(command);
Now i have set my commands bin directory in PATH variable by editing ~/.bash_profile file
export PATH=$PATH:<my commands bin directory>
it runs perfectly when i manually run the command by opening a new command prompt, but when i'm trying to run it using process command it fails as the PATH variable does not have my commands bin directory.
It seems when "Runtime.getRuntime().exec(command);" invokes a new bash shell it does not include or read ~/.bash_profile file.
I have also verified that the user is same when running manually and using java code.
Could anyone point out whats issue here?
Runtime.getRuntime().exec() doesn't search PATH by default. You will either need to find a different method that does this for you or implement it yourself by loading PATH, possibly parsing it, and then iterating through it to find the executable.
Related
I'm trying to execute a Java file I was given on Windows 10, inside of the Bash shell.
I open my command prompt. I enter bash.
I set
JAVA_CALL="C:/Program Files/Java/jdk1.8.0_192/jre/bin/java"
I try to execute the call, but to no luck. I read several threads on here and tried several things. I made sure my path includes both the Program Files x86 and the regular Program Files version of my JAVA.
I executed
sudo ln -s -f /mnt/c/Program\ Files/Java/jre1.8.0_192/jre/bin/java.exe /bin/java
To try and make a link to it. I cannot get it to wrong. It always tells me
-bash: C:/Program Files/Java/jdk1.8.0_192/jre/bin/java: No such file or directory
I am sure that file exists. Any ideas?
This question already has answers here:
How to install Java application to my linux system
(2 answers)
Closed 6 years ago.
I'm new to Linux and Ubuntu environment.
I'm calling a jar file like this :
java -jar app.jar /somearg /anotherarg
But I find this ugly and I want to call my jar file like :
MyApp /somearg /anotherarg
So I think I have to set a environment variable like MyApp = java -jar app.jar.
But I don't know how can I do it.If anyone can help me I'll be very pleased.Thanks.
Using
java -jar app.jar /somearg /anotherarg
instead of
myapp /somearg /anotherarg
saves you only 2 words, so not that much. Anyway if you run your application frequently it is a good idea to provide an alias. If you are using bash shell (echo $SHELL shows something like /bin/bash) then here's the command you can paste in your terminal:
alias myapp="java -jar /path/to/your/app/app.jar"
After that you can use
myapp /somearg /anotherarg
It is important to provide the whole path to your app.jar file if you run it from different locations. Also if you want that your alias is permament (i.e. it is always after you log in) just add the same line at the end of ~/.bashrc file, using for example:
pico ~/.bashrc
PS. You don't need slash symbol to provide parameters. In unix systems it is more common to use "-" to set argument options.
PSS. Unix systems are case sensitive and it is typical that command line programs are all written in lowercase.
This question already has answers here:
javac is not recognized as an internal or external command, operable program or batch file [closed]
(6 answers)
Closed 6 years ago.
I'm trying to run a basic "Hello World" app from the command line on windows 10 using
javac MyFirstProgram.java
but i receive
'javac' is not recognised as an internal or external command, operable program or batch
naturally, the first thing i did was google the problem and many solutions were presented, tried a few but nothing worked, has anybody else experienced this?
Steps to fix this error in windows 10/8/7
1.Check your javac path on Windows using Windows Explorer C:\Program Files\Java\jdk1.7.0_02\bin and copy the address.
2.Go to Control Panel. Environment Variables and Insert the address at the beginning of var. Path followed by semicolon. i.e C:\Program Files\Java\jdk1.7.0_02\bin; . Do not delete the path existent, just click in and go to the left end and paste the line above. Do not try anything else, because you just need to link your code to "javac.exe" and you just need to locate it.
3.Close your command prompt and reopen it,and write the code for compile and execution.
You need to add the location of your JDK to your PATH variable, if you wish to call javac.exe without the path.
set PATH=%PATH%;C:\path\to\your\JDK\bin\dir
Then...
javac.exe MyFirstProgram.java
OR, you can simply call it via the full path to javac.exe from your JDK installation e.g.
C:\path\to\your\JDK\bin\javac.exe MyFirstProgram.java
This question already has answers here:
‘ant’ is not recognized as an internal or external command
(11 answers)
Closed 7 years ago.
I am trying to set up the apache ANT in Windows7 and executed below commands in cmd.
set ANT_HOME=c:\Softwares\apache-ant-1.9
set JAVA_HOME=c:\Softwares\jdk7x64
set PATH=%ANT_HOME%\bin;%JAVA_HOME%\bin;
But, when i execute ant -version command it gives me below error. I cannot set the path from environment variables as i don't have admin rights to do so. How can i do it through command prompt only. What is wrong here ?
Error
'ant' is not recognized as an internal or external command,operable program or batch file.
If i give below command
echo %ANT_HOME%
i get the output as
c:\Softwares\apache-ant-1.9
Did you try setting the PATH variable as below?
set PATH=%PATH%;%ANT_HOME%\bin;%JAVA_HOME%\bin;
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Java execute a command with a space in the pathname
I am having following command
i_view32.exe C:\*.bmp /import_pal=C:\default.pal /convert=D:\temp\*.bmp
Which when i run through command prompt works fine.
I am trying to run the same command with the help of java.
Process p = Runtime.getRuntime().exec(System.getenv("ProgramFiles")+"\\IrfanView\\i_view32.exe c:\\*.bmp /import_pal= 1.pal /convert=d:\\temp\\*.bmp");
But i am not able to get Output in d:\\temp\\ Folder. Can any one suggest me where i am wrong.
Thanks in Advance..
Is there any other way to give "/" as i am using slash /import_pal=
2 your attempts are not exactly the same. I think that you executed command from command prompt when you were in c:\Program Files\IrfanView. When you are trying to run the same command from java you mention the full path. Since some programs are sensitive to current working directory I'd recommend you first to try to run the command from other directory (e.g. from c:) but specify full path.
If it works manually but does not work from java try to use ProcessBuilder instead of Runtime.exec(). Actually it is almost the same but it more Object Oriented and allows to specify working directory separately. I hope this will work for you.
If not try to play with quotes. Directory path 'c:\Program Files' contains space, so the path should be quoted.
Good luck.
Try to execute CMD
Example:
proc = Runtime.getRuntime().exec("cmd.exe /c dir");
It should work something like this, for your example it's a bit more complicated, but try it this way.