Uff... tried to google but with no result.
Hello everybody. I need to run via cmd.exe the next command from java programm (javascript syntax):
"/c cd c:\prb && Processing.bat c:\prb ext.dat auto"
it means i need to change current directory to c:\prb, write "Processing.bat c:\prb ext.dat auto" and press enter.
My Java code is:
String command = "cmd /c start cmd.exe /K cd c:\\prb Processing.bat c:\\prb prb ext.dat auto";
Process pr = Runtime.getRuntime().exec(command);
pr.waitFor();
but it doesn't work. I suppose that i miss some code between "c:\prb" AND "Processing.bat" in the command line.
Could anybody help me?
I can see at least one mistake: path c:\\prb Processing.bat contains space and therefore must be quoted:
"cmd /c start cmd.exe /K cd \"c:\\prb Processing.bat\" c:\\prb prb ext.dat auto"
I am not sure about c:\\prb prb ext.dat. Is it one path or 2 separate arguments? If it is one argument quote it too.
This time I will write a cope from my memory, I have used like 8 year ago :)
What is you trying to solve it is impossible, no that isn't the proper word, but: you want to launch cmd with a specified working directory, than launch a bat file.
Far as I remember:
Launch the .bat, but give a parameter other working directory
Or:
launch cmd (1 process)
give a param to execute the change directory
give another param to execute the bat file. All from Java, it is more complex.
How to do it it is another question.
here is what should be the command like if you do no specify the working directory:
String command = "cmd /c \"c:\prb\Processing.bat\"";
do not forget to change the working directory if you need. 8 years ago I did, sorry if I am robbing your time, just want to help
Related
I want to open a console window and write any command I want, from a string which varies depending on what I input. The problem is that no commands with a / or \ work because of the last / in the CMDs parameters.
I've tried removing and changing the parameters, but the CMD won't run correctly then.
Runtime.getRuntime().exec("cmd /c start cmd.exe /K" + command + "\"");
I Want to be able to run any command (From the command string), even commands like cd C:\ which contains a / or \
Commands containing slash or backslash work just fine in your code, with or without the extra \" at the end, e.g. c:\windows\system32\winver or c:/windows/system32/winver
C:\ doesn't work because that isn't a valid CMD command, which if not a builtin (like cd or set or if) or drive: (or :label) must be a filename (either a pathname, or a simple name which is searched on %PATH%) which is a program (.exe or on older versions .com) or is associated to a program or is a .bat file. If you run CMD interactively and enter C:\ it fails, and passing the same thing from Java can't make it work any better.
It is a valid 'command' in the Search-formerly-Run box, or the start command. Those, deliberately, accept some things that are not programs, and figure out how to 'open' them -- in particular they 'open' a directory name like C:\ by running Explorer. Using your code to run explorer c:\ does work.
I am running a batch (ScanProject.bat) file using java by following code
Process p= Runtime.getRuntime().exec("cmd /c start /wait ScanProject.bat "+ BaseProjDir+"\\"+jo.getString("Name")+" "+st.nextToken());
System.out.println("Exit value : "+p.waitFor());
And following is the batch file code :-
%2:
cd %1
ant -f ..\antbuild.xml analyse
exit
Batch file run successfully but problem is command prompt window do not closes automatically and hence Process do not terminated automatically and my program wait for infinite time to complete this process.Please suggest me any technique so that cmd exit after running ant -f ..\antbuild.xml analyse command.
Thanks.
cd /D "Full path of directory" or pushd "Full path of directory" with popd before exit is better to switch the current directory to any directory on any drive (cd and pushd/popd) or even to a network share (just pushd/popd). Run in a command prompt window cd /? and pushd /? for details.
cmd /C starts a new Windows command process with closing the process automatically after last command was executed. Run in a command prompt window cmd /? for details on options of Windows command interpreter.
start is a command to start a new Windows command process or a GUI/hybrid application in a separate process.
So what you do here is starting a new Windows command process which starts a new Windows command process.
Running in a command prompt window start /? outputs the help for this command. start interprets often the first double quoted string as title string for the new command process. This causes often troubles on command lines with at least 1 double quoted string. Therefore usage of start requires often an explicit definition of a title string in double quotes as first argument for start which can be even an empty string, i.e. simply "" as first argument after start.
As it can be read after running exit /? in a command prompt window, this command without /B always exits the current Windows command process immediately. So when ant.exe finished, the command process in which the batch file was processed is definitely terminated.
I'm having no experience on Java development, but in my point of view it should be enough to use the following execution command which does not need a batch file at all.
The Java code line
Process p= Runtime.getRuntime().exec("cmd.exe /C cd /D \"" + jo.getString("Name") + "\" && ant.exe -f ..\\antbuild.xml analyse");
should be enough to
start a new Windows command process,
set the current directory within this command process to the drive and directory specified by jo.getString("Name") which of course must return a directory path with drive letter and using backslashes as directory separators, and on success
execute ant in this directory with the specified parameters
with terminating the Windows command process automatically after console application ant.exe finished if ant.exe is a console application.
I'm not sure if cmd.exe /C is needed at all.
I suggest to test this command first manually from within a command prompt window. Then use it in the Java application if really working and producing the expected result. And finally I would further test if cmd.exe /C is needed at all in Java code.
See Single line with multiple commands using Windows batch file for details about the operator && to run a command after previous command was successful. And see also Why do not all started applications save the wanted information in the text files as expected? for an explanation of console / GUI / hybrid application.
NOTE: There is also Java Runtime method exec(String[] cmdarray, String[] envp, File dir) to execute a command like ant.exe with its parameters -f and ..\antbuild.xml and analyse in the directory defined with third parameter which might be better for this task.
Swap out exit for taskkill, assuming you do not have any other cmd processes running. Not very graceful but it will get the job done.
%2:
cd %1
ant -f ..\antbuild.xml analyse
taskkill /im cmd.exe
I'm using apache-commons-exec to execute some commands in a Java application.
When I execute 'ls -la /home/user' it works great.
But I need to execute something like this
./setEnvsOfTypeXXX.sh; ./setEnvsOfTypeYYY.sh; ls -la /home/user
I enter the command into the CommandLine object and it doesn't work.
It returns an empty string and -559038737 exit code.
Because the nature of the environment and the scripts (the firsts ones sets some needed environment variables); i can not put all the call into a script o
I've tried many solutions (like surround all the command with quotation marks like "'" or use the PumStreamHandlet input stream) but nothing has worked so far...
Anyone has an idea ?
try
sh -c '. ./setEnvsOfTypeXXX.sh; . ./setEnvsOfTypeYYY.sh; ls -la /home/user'
As your command
Two things I'm guessing you need here.
First if you are setting enviroment variables you probably need to use .
Second you want to run a shell and get the shell to exec the shell scripts and then run the following command, all in the same context
I tried this code
cmdLine = new CommandLine("/bin/bash");
cmdLine.addArgument("-c");
cmdLine.addArgument(new StringBuilder().append("'").append(command).append("'").toString());
And even with command = "ls";
There is an error
bash: ls: No such file or directory
fun fact: in windows this works ok !
cmdLine = new CommandLine("cmd.exe");
cmdLine.addArgument("/c");
cmdLine.addArgument(new StringBuilder().append("\"").append(command).append("\"").toString());
logger.info("Command win line: cmd.exe /c \""+command + "\"");
I totally out of options now !!!
I got a workarround: create a temporal sh file with the command, putting shebang on firts line and giving permissions, executing this file in one command line, get result and output, for last delete temporal file...
and it works !
I managed to change system time and date using runtime in java. However I have to run this two commands one at a time opening two command prompts instead of one because if run both commands at the same time the command prompt gets them as one invalid command
//this is the working code that opens two cmd`s:
Runtime rt = Runtime.getRuntime();
rt.exec("elevate.cmd cmd.exe /c time 11:30");
rt.exec("elevate.cmd cmd.exe /c date 02-04-2012");
//this is the code that I think it should open one cmd and execute both of the commands
Runtime rt = Runtime.getRuntime();
rt.exec("elevate.cmd cmd.exe /C time 11:25 /C date 02-05-2012");
But the cmd is returning "The system cannot accept the time entered".
Note: the elevate.cmd is a batch file I use it to run the cmd as administrator(win7) and you can download it from here.
How can I make the system change both time and date by opening cmd once? Or what other choices do I have?
Try solutions from this SOq:
How to execute cmd commands via Java
Basically, create a process and then "write" commands to it as if you were an user typing them. I don't have Win7 to test (don't know how it will behave in combination with elevated privilege prompts), but it works for me on Ubuntu 11.10 - hope it works for your case, too.
Alternatively, you can make another .cmd file (called e.g. changedt.cmd) that will contain the two commands:
elevate.cmd cmd.exe /c time 11:30
elevate.cmd cmd.exe /c date 02-04-2012
and then run it instead:
Runtime rt = Runtime.getRuntime();
rt.exec("changedt.cmd");
This should work in any case, as you seem to be successfully running pretty much the same thing, but adds another .cmd file.
Hope this helps.
In Windows, I made a small script to compile and then run a Java application:
javac helloWorld.java
java helloWorld
helloWorld prints "Hello, world!" and then the command prompt closes immediately. What I want to happen is for the program to execute then have a new line on the command prompt ready to go.
EDIT: 1 more stipulation. It needs to be just one batch file, not a batch file calling another one.
Append the line:
cmd
...at the end of your batch file.
you need to start an instance of cmd.exe and just let it run.
You could start the script like this:
cmd /K script.cmd
This will keep the cmd shell open.
You can accomplish this by creating a desktop shortcut with the given line.