Use several DOS command from java program
Using "Runtime.getRuntime().exec(toto.bat)" I have no problem to run a Command in my DOS window...
But the problem appear when I want to call another command in this same window from myApp.java...
In fact after executing the first command I find no way to have another access to this DOS window...
Is there any way to set the Process created by calling exec() method as the default system??
Hope my explications are clear...
Does someone have the solutions...this will help me so much...
bilbou.
ok but I have found no forum which resolve my problem...can you give more details about what you think concerning the solution of my pb...
bilbou.
Similar Messages
-
Problem while running dos command from java program
Dear friends,
I need to terminate a running jar file from my java program which is running in the windows os.
For that i have an dos command to find process id of java program and kill by using tskill command.
Command to find process id is,
wmic /output:ProcessList.txt process where "name='java.exe'" get commandline,processid
This command gives the ProcessList.txt file and it contains the processid. I have to read this file to find the processid.
when i execute this command in dos prompt, it gives the processid in the ProcessList.txt file. But when i execute the same command in java program it keeps running mode only.
Code to run this command is,
public class KillProcess {
public static void main(String args[]) {
KillProcess kProcess = new KillProcess();
kProcess.getRunningProcess();
kProcess = new KillProcess();
kProcess.readProcessFile();
public void getRunningProcess() {
String cmd = "wmic /output:ProcessList.txt process where \"name='java.exe'\" get commandline,processid";
try {
Runtime run = Runtime.getRuntime();
Process process = run.exec(cmd);
int i = process.waitFor();
String s = null;
if(i==0) {
BufferedReader stdInput = new BufferedReader(new
InputStreamReader(process.getInputStream()));
while ((s = stdInput.readLine()) != null) {
System.out.println("--> "+s);
} else {
BufferedReader stdError = new BufferedReader(new
InputStreamReader(process.getErrorStream()));
while ((s = stdError.readLine()) != null) {
System.out.println("====> "+ s);
System.out.println("Running process End....");
} catch(Exception e) {
e.printStackTrace();
public String readProcessFile() {
System.out.println("Read Process File...");
File file = null;
FileInputStream fis = null;
BufferedReader br = null;
String pixieLoc = "";
try {
file = new File("ProcessList.txt");
if (file.exists() && file.length() > 0) {
fis = new FileInputStream(file);
br = new BufferedReader(new InputStreamReader(fis, "UTF-16"));
String line;
while((line = br.readLine()) != null) {
System.out.println(line);
} else {
System.out.println("No such file");
} catch (Exception e) {
e.printStackTrace();
return pixieLoc;
} when i remove the process.waitFor(), then while reading the ProcessList.txt file, it says "No such file".
if i give process.waitFor(), then it's in running mode and program is not completed.
Colud anyone please tell me how to handle this situation?
or Is there anyother way to kill the one running process in windows from java program?
Thanks in advance,
SathishHi masijade,
The modified code is,
class StreamGobbler extends Thread
InputStream is;
String type;
StreamGobbler(InputStream is, String type)
this.is = is;
this.type = type;
public void run()
try
InputStreamReader isr = new InputStreamReader(is, "UTF-16");
BufferedReader br = new BufferedReader(isr);
String line=null;
while ( (line = br.readLine()) != null)
System.out.println(type + ">" + line);
} catch (IOException ioe)
ioe.printStackTrace();
public class GoodWindowsExec
public static void main(String args[])
try
String osName = System.getProperty("os.name" );
String[] cmd = new String[3];
if( osName.equals( "Windows 95" ) )
cmd[0] = "command.com" ;
cmd[1] = "/C" ;
cmd[2] = "wmic process where \"name='java.exe'\" get commandline,processid";
} else {
cmd[0] = "cmd.exe" ;
cmd[1] = "/C" ;
cmd[2] = "wmic process where \"name='java.exe'\" get commandline,processid";
Runtime rt = Runtime.getRuntime();
System.out.println("Execing " + cmd[0] + " " + cmd[1]
+ " " + cmd[2]);
Process proc = rt.exec(cmd);
System.out.println("Executing.......");
// any error message?
StreamGobbler errorGobbler = new
StreamGobbler(proc.getErrorStream(), "ERROR");
// any output?
StreamGobbler outputGobbler = new
StreamGobbler(proc.getInputStream(), "OUTPUT");
// kick them off
errorGobbler.start();
outputGobbler.start();
// any error???
int exitVal = proc.waitFor();
System.out.println("ExitValue: " + exitVal);
} catch (Throwable t)
t.printStackTrace();
}when i execute the above code, i got output as,
Execing cmd.exe /C wmic process where "name='java.exe'" get commandline,processid
and keeps in running mode only.
If i execute the same command in dos prompt,
CommandLine
ProcessId
java -classpath ./../lib/StartApp.jar;./../lib; com.abc.middle.startapp.StartAPP 2468
If i modify the command as,
cmd.exe /C wmic process where "name='java.exe'" get commandline,processid > 123.txt
and keeps in running mode only.
If i open the file when program in running mode, no contents in that file.
If i terminte the program and if i open the file, then i find the processid in that file.
Can you help me to solve this issue? -
Problems to execute "cd" DOS command from Java program
Anyone try to exec command "cd.." or "cd directory", it doesn't have any action. No exceptions, just don't do anything.
Runtime.getRuntime().exec("cd\\");
Runtime.getRuntime().exec("cd hello");
However, when I try the following, it is working fine. Any ideas???
Runtime.getRuntime().exec("explorer");
Runtime.getRuntime().exec("javac Test1.java");That's because cd is built into the command
interpreter in Windows. Runtime.getRuntime().exec()
can only be used to launch external programs. This
article should get you going:And that's not the only reason why you can't do this.
When you do
Runtime.getRuntime().exec("cd c:\\");
(or, heck, "cmd /c cd c:\\"): the new command that you spawn will cd to the destination specified, and then exit. The parent process (your java program, that is launching this command) won't go anywhere - the "cd" doesn't affect it.
The only way to change your OS directory in Java is to invoke native code. And even then, the effects are undefined (in terms of how your CLASSPATH will be affected, etc.). -
How to execute dos command in Java program?
In Perl, it has System(command) to call dos command.
Does java have this thing?
or any other way to execute dos command in java program?
Because i must call javacto compile a file when the user inputed a java file name.Look in the Runtime class, it is implemented using the Singleton design pattern so you have to use the static method getRuntime to get its only instance, on that instance you can invoke methods like
exec(String command) that will execute that command in a dos shell.
Regards,
Gerrit. -
hi all
how do i run dos commands from java??????????Using Runtime#exec().
Also see http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Runtime.html -
Hi, I need to perform a DOS command from Java (using WFC if needed)...
I have no idea, so please help!
Thank you.OH OH OH WAIT! It is an Applet! i think there will be some security issues here! Sure enough you can't execute commands on the system from an applet unless you make some changes on the java.policy files on the client machine (that means, if your applet is in a web page, every machine which view your page).
-
May I ask how I should write if I want to execute a dos command in java? The command is :
"C:\Program Files\Real\realproducer\rmeditor -i abc.rm -d abc.txt". Should I use array?
Thanks!why an array ?
String command = "C:\\Program Files\\Real\\realproducer\\rmeditor -i abc.rm -d abc.txt";
Process p = Runtime.getRuntime().exec( command );what about the seach function on the left side ? ;-)
tobias -
Invoking dos commands from java
hi all,
I just want to invoke dos prompt from java.I know it can be done using "Runtime.getRuntime()" command.But when i try to create a directory inside another directory,how can i specify the path of the destination directory.i googled as well as tried different things.But i failed to make it up.Please help me solve this problem.
thanks in advance
RegardsHai,
Thanks for your respond.
If I give "cmd" command only, I am getting dos command window, but I didn't get the prompt like
c: or d:
How can I do it?
Expecting more helps
Joseph -
How to invoke dos shell from java program
Hi,
I'm not able to invoke dos shell from java.
Can any one help me in this issue.
I'm providing the source code below:
try{
Runtime.getRuntime().exec("cmd.exe")
catch(IOException e) {
System.out.println(e.getStackTrace());
ThanksDoes it throw a different exception?
Or does it just do nothing at all?
It does nothing at all[/b
Is this a standalone Java app?
Or a Java Applet running via a webbrowser? [b]It's a standalone application -
Error using DOS Command from Java
Hi,
I am using the following code to execute the DOS command to delete a file from Java.
cmd = "del " + fileName;
Runtime run = Runtime.getRuntime();
Process proc = run.exec(cmd);
Getting the following exception during runt time.
Exception = java.io.IOException: CreateProcess: del D:\LAWDOCS\P50074\12\3\36857.3 error=2 --
Can some body throw light on this?.
Thanks,
Jeyaraman R"del" is not an actual executable file, but instead a command in the command interpreter. So you can't just execute del like that, you have to run the command interpreter and tell it to execute the del command.
If you are on windows NT, 2000 or XP, replace your command with "cmd /C del " + filename.
If you are on Windows 95, 98 or ME, use "command /C del " + filename. (I am not quite sure if the switch for this is called /C on old windows version.)
(But why not use File.delete()?) -
Executing a dos command from java in windows nt
i need to execute the
" net send"
command from the command line in windows nt, from within my java program... is there any way that i can execute cmommands in the dos command shell? or rather, does anybody know a way i can send a message ( in windows ) from java, from one computer to another over the lan?? I cannot have a java programming running on all the other computers, so i cannot make my program serve, it must just send the messges using a lan command....
(the net send command just sends a message in the form of a pop up box on the receiver's screeen)
thanks a bunch guys!!!
Thanks a bunch guys!!!!you can do that with exec() in Runtime.
Runtime rt = Runtime.getRuntime();
Process proc = rt.exec(command here);but before you do that you need to read this:
http://www.javaworld.com/jw-12-2000/jw-1229-traps.html
hth
partha -
How to execute system command from java program
Hi all,
I want to change directory path and then execute bash and other unix commands from a java program. When I execute them separately, it's working. Even in different try-catch block it's working but when I try to incorporate both of them in same try-catch block, I am not able to execute the commands. The change directory command works but it won't show me the effects of the bash and other commands.
Suggestions??The code I am using is....
try
String str="cd D:\\Test";
Process p=Runtime.getRuntime().exec("cmd /c cd
"+str);your str string is already having cd in it but again you ar giving cd as part of this command also please check this,i will suggest you to remove cd from str
Process p1=Runtime.getRuntime().exec("cmd /c mkdir
"+str+"\\test_folder");you should say mkdir once you change your path,but here you are saying mkdir first and then cd D:\Test(this is because of str)..please check this
Process p2=Runtime.getRuntime().exec("cmd /c bash");
Process p3=Runtime.getRuntime().exec("cmd /c echo
himanshu>name.txt");
catch(IOException e)
System.err.println("Error on exec() method");
e.printStackTrace();
Message was edited by:
ragas -
Execute DOS command from java application
Hello,
I want to execute a DOS command (MOVE, in order to move an image from a folder to an other) from a java application. How can I do this?
FrancescoYes I have tested it and it is working but only when executing a bacth file. For instance:
Runtime rt = Runtime.getRuntime();
try{
Process proc = rt.exec("move.bat");
}catch(Exception ex){
ex.printStackTrace();
}and the command in move.bat is:
move c:\\temp\\*.gif C:\\temp2
You don't have to use double slashes in batch files, only in Java. But anyway it is working both ways.
It is not working when you try to execute the command without the batch file:
Process proc = rt.exec("move c:\\temp\\*.gif C:\\temp2"); -> this will not work.
It should work. Try to execute another command to see what happens. -
How can I run a dos command from java on windows 98, 95?
The usage of cmd.exe in the java program to run a dos command on windows 98. 95 doesn't work as the equivalent command is command.exe
But using the command.exe in the java program makes my program to hang.hi,
As u mentioned, u cannot use the cmd.exe in win9x environment as cmd.exe is specific to windows NT, you can use the command.exe without any hitches.
for eg
java.lang.Runtime.getRuntime().exec("start command /K a.bat"); should run the batch file a.bat..
if the problem persists, try posting the snippet of code that you are using.
cheerz
ynkrish -
Hi ,
I would appreciate if anyone could tell me how to run DOS command such as "del" using JAVA language .Thank you.<steps onto soapbox>
Surely for something like 'del' we should be advocating a non-OS specific method so we don't lose sight of Java's cross platform abilities.
If it has to run an OS specific thing fine, but please look for a non OS specific solution first.
<steps off soapbox>
Maybe you are looking for
-
How to get the HttpServletRequest object in a jsp
Hi, I am a little confused here. Am trying to use the HttpServletRequest object in my jsp. I have set up just a simple test page jsp. How can i use the HttpServletRequest object to get some information about the request for example using the method g
-
Check the G/L account in overall limit of Purchase order - ME22
Hi I should check the G/L account in overall limit of Purchase order ( modify - transaction ME22 ) Someone knows an exit or badi in witch i may check this ? There is an enhancement that i can work on this information? Thank you
-
I need to remote program an HP4193A vector impedance meter, but I do not have a manual nor can I find a Labview driver. I basically need to know the gpib syntax for setting the frequency. I can read the values just by sending a read command. Please h
-
I need help with this code PLEASE!!!! Stop_btn.addEventListener(MouseEvent.CLICK, fl_ClickToPauseVideo_3); function fl_ClickToPauseVideo_3(event:MouseEvent):void assets/01_01_010.fla.pause(); Play_Btn.addEventListener(MouseEvent.CLICK, fl_ClickTo
-
salam 2all i have got this error each time i open my computer the error is { java.exe has generated errors and will be closed by windows. You will need to restart the program. An Error log is being created Thanks 4ur help Salam2all