Executing cmd.exe commands from Java - java

I'm trying to read a file from the user, in which each line is a cmd.exe command, and run it (it's okay to assume the commands are legal), but when I give a command like echo hi, I get runtime exception error:
Exception in thread "main" java.io.IOException: Cannot run program "echo": CreateProcess error=2, The system cannot find the file specified
I'm trying to run the commands like this:
Runtime.getRuntime().exec(command);
where command = "echo hi". This does work for commands like regedit though, so it seems the runtime I'm getting is like the "run" window and not cmd. Is there a way to run these commands?

That's because echo is not an external executable command (i.e., there is no echo.exe file on your hard disk, unless you put it there yourself). It's an internal command of the shell.
You'll probably find that you need to execute something like:
cmd.exe /c echo hello

Related

How do i execute a shell script in eclipseIDE?

i want to execute a sheel on my Eclipse, and am unable to do so, is it because eclipse does not support shell script or do we any plugin for it.
I am executing it like this, and Name is Argument than i am passing to my script as search pattern.
$ String[] commands = {"C:/Users/shreyas.a.ramesh.DIR/workspace/Shellweb/src/Search_file.sh", Name};
And error I get is as follows
java.io.IOException: Cannot run program
"C:/Users/shreyas.a.ramesh.DIR/workspace/Shellweb/src/Search_file.sh": CreateProcess error=2, The system cannot find the file specified
Sorry about the code indentation.
This isn't a Java or Eclipse problem. Shell scripts aren't executable binaries. You'll have to run the shell binary itself with whatever arguments it needs to interpret your script.

Execute cmd commands from inside a java program

I am trying to execute cmd commands inside a java program using the following code
String command = "clingo F:\\clingo\\food1.lp F:\\clingo\\fooddata.txt"
+ " 0"+" >>F:\\clingo\\foodout.txt";
Process p1 = Runtime.getRuntime().exec(command);
This is executing in java without any exceptions, but the actual command is not running. If the command is run it should create text file foodout.txt in the location mentioned. Nothing is happening.
The actual command is
clingo food1.lp fooddata.txt 0 >>foodout.txt
clingo is a windows executable program. This command works fine when run in command prompt. I want to run this inside java program from click of a button. I have set environment variable for clingo. Clingo and this java project are in the same directory.
Before this i tried below code
String[] command = {"clingo", "food1.lp","fooddata.txt", "0", ">>foodout.txt"};
ProcessBuilder builder = new ProcessBuilder(command);
builder.directory(new File(WorkingDirectoryArea.getText()));
Process process = builder.start();
where Workingdirectoryarea contains the directory location for commands to be run. This code does nothing.
Can someone guide me or provide code sample on how to run the cmd command inside this java program. I am using Netbeans IDE. Thanks.
you said your command works with a command prompt. OK. If you look closely, the command window has a path entry (cmd= echo %PATH%). That's the difference between executing a command in a command window and executing a java process. You have 2 options.
1. Add the path to the process.
2. Add the path to the clingo command (i.e. "f:\path\clingo.exe ...)
Item 1 is especially needed when using dos commands. To add a path environment:
Runtime.getRuntime().exec not finding file in java environment
You are redirecting standard output to a file. This is not part of the command nor a command line parameter. Is the command interpreter that handles this.
You must invoke the command interpreter to run your program like this:
String command = "cmd /c clingo F:\\clingo\\food1.lp F:\\clingo\\fooddata.txt"
+ " 0"+" >>F:\\clingo\\foodout.txt";
Process p1 = Runtime.getRuntime().exec(command);
Note the cmd /cpart which invokes the command interpreter to run your command like you would do on a Windows terminal.
On Linux it would be sh -c or whatever shell you like.
EDIT 1
When running the command, clingo.exe must be in your path or it must be in the default directory for the Java interpreter. If not, you should give the full path to the executable, like this:
String command = "cmd /c F:\\clingo\\clingo F:\\clingo\\food1.lp F:\\clingo\\fooddata.txt"
+ " 0"+" >>F:\\clingo\\foodout.txt";
Try to run
F:\\clingo\\clingo F:\\clingo\\food1.lp F:\\clingo\\fooddata.txt 0 >> F:\\clingo\\foodout.txt
at a Windows prompt and see if it works as expected. If it works it also should work when run from a Java program. Please, replace the clingo path with the right one for your environment.
Your command must be like this: java -jar yourExecuteable.jar yourParameter
In your case: java -jar clingo.jar food1.lp fooddata.txt 0 >>foodout.txt

How to run batch file with parameters in java?

I have created a jar file called test.jar under C:\jars. I have JAR file under the same location named run.bat and it contains the below code -
#echo off
set exec_path=C:\jars java -cp %exec_path%/test.jar; com.mycomp.myapp.MyProgram "%1"%*
#echo on
It is running successfully from command prompt with parameters.
Now I would like to run it from another JAVA program.
Please suggest.
Thanks!
I've encountered this issue before. The answer is that you have to run cmd.exe or bash or whatever shell you've got, then feed in the command to that process via the process input/output streams.
Process p = Runtime.getRuntime().exec("cmd");
p.getOutputStream().write("mybatch.bat\n");

running busybox through a .bat using java

I am trying to execute ls command through busybox.
I am creating a .bat file to execute this command which i am calling through .java
However, i am not able to execute commands one after another in .bat file.
This is the contents of my .bat file
"C:\Documents and Settings\Some Directory\Android\android-sdk\platform-tools\adb.exe" shell
/data/busybox/busybox ls
what i think that once i start the shell through the first line of my .bat, the control from the shell is lost hence second command is not executed.
Because if i write my .bat file as
"C:\Documents and Settings\Some Directory\Android\android-sdk\platform-tools\adb.exe" shell ls
it works fine.
I need to write commands in my .bat file so that they exceute one after the another.
I have tried using CALL before each commands in .bat, still it does not work.
I have tried to use multiple .bat, still a fail cause.
Can someone please help me on this?
Thanks a ton.
I am unable to test this myself with ADB at this moment, but this works for other programs that have an input buffer. I will try to verify this tonight, but if anyone else confirms it before then, leave a comment.
#echo off
( echo shell
echo /data/busybox/busybox ls
) | adb.exe

How to run Windows command from Java

I would like execute the following command with Java:
"C:\Program Files\MySQL\MySQL Server 5.5\bin\"mysqldump -u root
--password=xxxx --routines database > C:\Users\john\Desktop\backup.sql
The command works perfectly when I use the Windows cmd.exe but not with my Java application.
cmd = "\"C:\\Program Files\\MySQL\\MySQL Server 5.5\\bin\\\"mysqldump
-u root --password=xxxx --routines database > C:\\Users\\john\\Desktop\\backup.sql
Runtime runtime = Runtime.getRuntime();
process = runtime.exec(cmd);
I got the following error:
"C:\Program Files\MySQL\MySQL Server 5.5\bin\"mysqldump -u root
--password=xxxx --routines database > C:\Users\john\Desktop\backup.sql java.io.IOException: Cannot run program ""C:\Program": CreateProcess
error=5, Access is denied
Do you have any idea?
Thank you.
You're trying to use a redirect (>) which is a function of the shell.
You don't have a shell, you're exec'ing the mysqldump process directly.
You either need to read the output stream from the process object and write it to a file yourself, or exec the windows shell to execute mysqldump for you along with the redirect.
Error 5 is either 1) cannot access the file/folder or 2) don't have permissions.
Looks like the space in your 'Program File' directory name may be causing trouble causing trouble. Check where it says "Cannot run program ""C:\Program". This is obviously not a reference to the command you are trying to run. What I would do is add MySQL to your path and then just call the mysqldump command without the path to it. If you don't want to do this, you can define a system variable and reference it with %MYSQL_HOME%\mysqldump.
Once you have the path resolved, if this still doesn't work, you'll want to look at the permissions for the command. You'll need to make sure that the process has permission to execute the command. I think this in local admin groups or something like that.
Dollars to donuts its your path tho.

Categories

Resources