How to execute commands using java [duplicate] - java

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.

Related

How to go into particular Directory (cd) and perform bash command(python server) in it through java?

What I want to do is: first go into some directory and start my server in the same directory for that I need to use cd directory path but I read previous threads and I got that I can’t use cd command since it is basically a system call rather than a command like ls.
My only aim is to go somehow in my desired directory chosen by Directorychooser and start python -m SimpleHTTPServer. I tried all possible answers like using sh -c and all other solutions provided to question like how do I use cd command in java. Is this possible without using cd.
I also have address chosen by directorychooser into string address . Also how do I execute any other command in that directory. I am not sure if Run.exec or runtime.exec can still help here as it doesn't do CD I tried !
Previous answers questions doesn't explain well how to use processbuilder and other thing, Any help appreciated .
Tried these as well -
Runtime.getRuntime().exec(sh -c 'cd /path/to/dir && ProgToExecute
Yes It is possible , I was also stuck into the same problem and I couldn’t do cd anywhere and this solution is how I solved it .You can achieve what you want to do with ProcessBuilder. (Runtime.exec() didn’t help me there as well , as suggested by previous answers on stackoverflow)
Here is how you can achieve it :
Build the process
Give the path
Start the process
In code :
ProcessBuilder pbuild = new ProcessBuilder(“command”); //split into number of tokens like following example
In your case are 3 like this :
ProcessBuilder pbuild = new ProcessBuilder("python" , "-m", "SimpleHTTPServer");
Now set the path where the process is to be executed i.e. you are indirectly performing cd directory-path here
pbuild.directory(new File(address));
(You said you have your path stored in address ,I am assuming, so)
Now start the process 
Process proc = pbuild.start();
Done! Compile and run . Command will be executed with cd.

PATH variable not been read from ~/.bash_profile [duplicate]

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.

Starting Java app from cmd works, from batch not

The Problem is as follows:
I have a Java Application packed in a ".jar"-File.
I can start the App (Swing Application) by either
doubleclick on the jarfile
commandline java -jar MyApp.jar
However when I put the commandline command into a batchfile, it will tell me
Unable to access jarfile MyApp.jar
The batch file is in the same directory as the jar. Just where I also started from cmd-line.
I tried to call the bat with Administrator-rights and from an admin-shell - no effect.
I'm working on a Windows 7 (64bit) machine. Any hints will be appreciated.
EDIT
You asked for it :) Here is the batch file:
#ECHO OFF
java -jar MyApp.jar
#PAUSE
EDIT 2
Invoking the batch by double-click and from cmd has the same effect.
Changing the command to
java -jar "C:\Full\Path\MyApp.jar"
has no effect.
EDIT 3
I added SET Path=%Path%;C:\Path\To\Jar to the batchfile. No effect.
I also tried using double backslashes as suggested by Chris' link and supported by Viruzzo. Also no effect.
When you double click on .bat file, the working directory is not set to where the .bat file is, but is usually something like %AppData%. To make it work you need to put the full path to the file inside the bat (i.e. java -jar C:\path\to\jar\MyApp.jar).
I just had to put my path in quotes, even though it had no spaces or odd characters.
java -jar C:\Users\Jacob\executables\some-file.jar failed, but
java -jar "C:\Users\JStamm\executables\some-file.jar" worked.
This isn't the answer to your specific problem, but this will allow you to troubleshoot the problem so you will find an answer. Adding a sleep command will let the command prompt sit, so it doesn't close right away.
ping 1.1.1.1 -n 1 -w 10000
(sleep for 10 seconds)
Make sure you don't have the hide extension option enabled in windows. Maybe the name of your jar ends with .jar.jar? Are you sure the path to the jar is correct?
I havent't yet figured out why, but now it works:
The first batchfile was a copy of another and I just changed the contents to fit my needs.
NOW I renamed it to MyApp.bak and wrote a new MyApp.bat from scratch.
As soon as I find out, what the difference was, I'll update this answer.
UPDATE
It happened again with another batch. This time, we got the reason: For some reason, the batch file did not inherit rights when copied. Creating a new one in the folder had the correct rights. So, in the end it was indeed a rights-issue.

Run some arguments in command line from java (using Eclipse)

Very basic command-line related question:
I have never tried to run anything in command line from java before and am struggling with the basics - other online information doesn't seem to work for my example, or I'm not understanding it.
In command line this is what it looks like:
C:\gnuplot\binary>gnuplot 15FebPlotFile.gp
All I have to do in command line is navigate to the correct file location (C:\gnuplot\binary) and then type gnuplot 15FebPlotFile.gp and it runs the thing I need (which simply generates a PDF and saves it to that file location)
I've seen people use Runtime and Process like on this site http://www.java-tips.org/java-se-tips/java.util/from-runtime.exec-to-processbuilder.html but I don't understand how I call the various command, like cd C:\gnuplot\binary and then from that location get it to run gnuplot 15FebPlotFile.gp.
If anyone could give me any advice on an approriate site to look at or some lines of code that might help me I'd be really greatful.
Thank you
You can work with ProcessBuilder, and then you can set the working directory of the process using ProcessBuilder#directory(File dir):
ProcessBuilder processBuilder = new ProcessBuilder("gnuplot", "15FebPlotFile.gp");
processBuilder.directory(new File("C:\\gnuplot\\binary"));
Process p = processBuilder.start();
I hope here you can find some code examples and solutions
Run command prompt from java?
changing the working-directory of command from java

How do I open a .bat containing a GUI in a java program in Linux?

everyone. I'm quite new here so please be tolerant if I make any mistakes.
I have a .bat file containing a command line to open up a .jar file that contains a program that has a GUI in it. The only line that's in the .bat file is:
java -jar "NewServer.jar"
I've been trying to use Runtime() to get this to run, but most the instructions I find to open a .bat file in a java program are for Windows. I'm currently using Fedora 12 (don't tell me to upgrade, I can't) if that makes a difference and programming using Eclipse. I also found this ProcessBuilder thing, but I couldn't get it to work so unless you have very explicit directions on how to use it, please don't include it in your answer. I would much rather use Runtime. It looked simpler.
Here's my code to test using Runtime in a java program. I'm hoping that if I can get this to work, I can get it to work in my real program.
import java.io.IOException;
public class testbat {
public static void main(String[] args) {
Process proc = null;
try {
proc = Runtime.getRuntime().exec("./ myServer.bat");
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("Cool");
}
The last line is just there for me to see if the program actually ran in case the GUI doesn't open. Also, I've already tried many combinations of things to include in the area after ".exec". I've tried using a path like "~/user/workspace/ProjectServer/dist/myServer.bat" to no avail.
I also already know that .bat files are for windows, but I'm able to execute it in linux, so I don't know if that makes a difference. I also tried using a .sh file the same way and it didn't work.
Please bear in mind that I'm not that great at Java, but I had to use it for this particular program, so if your answers could be really descriptive that would be awesome.
Just take that line out of the bat file, and run it. Yo're making it too hard.
$ java -jar "NewServer.jar"
will work. The quotes aren't necessary, so
$ java -jar NewServer.jar
will work as well. If you want to have the equivalent of your bat file, create a file named, say, run_newserver containing that line. Change its mode to executable:
$ cat > run_newserver
java -jar NewServer.jar
^D
$ chmod a+x run_newserver
$ ./run_newserver
Ideally, since you shouldn't have scripts without comments, do this. In your favorite editor, create a file run_newserver containing
#!/usr/bin/env bash
java -jar NewServer.jar
and chmod that. The line with #! -- often called a "shebang line" -- is UNIX magic that lets you say what interpreter you want. The program env in usr/bin finds your program and runs it (needed because different systems put bash in different directories.)
You could even put explanatory comments in the file too.
I'm a little unclear why you want to use Runtime#exec to run it at all -- it seems you'll just need a shell script to start that program.
Why are you using Java to run a Batch file, that in turn runs a Java program? Why have Batch in the loop at all? Just put the jar in your classpath and call it directly.
Batch (.bat) files are only for Windows environment. So, Try using shell script
Process proc = Runtime.getRuntime().exec("myServer.sh");
Just open up terminal and do this
vi /dir/to/exec/exec.sh
tap "i" and write this
#!/bin/sh
java -jar "NewServer.jar"
or if you want to run it in the background
#!/bin/sh
java -jar "NewServer.jar" & > /tmp/JavaServer.log
hit esc and type ":wq" and you have saved the file.
type this into the terminal
chmod +x /dir/to/exec/exec.sh
this give executable privileges and then you should run the file like
sh /dir/to/exec/exec.sh
Process is only initialized by your first call. You need to run:
proc.waitfor();
to get it to actually run your app.

Categories

Resources