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,
Sathish
Hi 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?
Similar Messages
-
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. -
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> -
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 -
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.). -
Error while running DOS command in Java
Hi all,
I use "runtime" class to execute a DOS command "copy C:\\pathname\\*.* C:\\backupPathname\\". But I saw an error msg "CreateProcess: copy C:\pathname\*.* C:\backupPathname\ error=2".
I did create a folder C:\backupPathname\ before running the DOS command.
Pls help me all Java genius.I ended up writing a small 'c' program and pasing the command in to it from java. The problem seems to be that in dos, unlike unix, the copy command is not a program, rather the command is interpreted by the command.com program.
-
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 -
Running ls command from Java stroed procedure no output
Hi ,
I am trying to run ls command from java stored procedure in oracle
Process p = Runtime.getRuntime().exec("ls");
BufferedReader stdInput = new BufferedReader(new
InputStreamReader(p.getInputStream()));
BufferedReader stdError = new BufferedReader(new
InputStreamReader(p.getErrorStream()));
// read the output from the command
System.out.println("output of the command run:\n");
while ((s = stdInput.readLine()) != null) {
System.out.println(s);
from java stored procedure in oracle.
i get output of println statments but it does not go into while loop to print from stdInput.
Result of running Java stored procedure is -
output of the command run:
Call completed.
when i run the program on client side it works fine.
Has anybody tried this from java stroed procedure.
Thanks,
JagJag,
Actually, the question of whether it works for me seems to depend on the version of the OS (or Oracle). On RedHat Linux (Oracle 8.1.6) it didn't work at all, but on Solaris (Oracle 9.0.2) it did. Here's the output from that run:
SQL> /
output of the command run:
init.ora
initDBPart9i.DBPSun01.ora
initdw.ora
lkDBPART9I
orapw
orapwDBPart9i
spfileDBPart9i.ora
Done
PL/SQL procedure successfully completed.
But, I did need to change a line of your code to this:
Process p = Runtime.getRuntime().exec("/usr/bin/ls");
your original was:
Process p = Runtime.getRuntime().exec("ls");
You might consider, if possible, use of some of the Java File classes instead of ls, as this might make things more predictable for you. There were some examples in oramag.com a few months ago, but they were pretty simple (you might not need them).
Hope this helps,
-Dan
http://www.compuware.com/products/devpartner/db/oracle_debug.htm
Debug PL/SQL and Java in the Oracle Database -
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. -
How to run DOS command in Java environment?
Can i run DOS command in Java environment?
I did like this:
Runtime r = Runtime.getRuntime();
r.exec("cmd.exe");
r.exec("set classpath=%CLASSPATH%;.\\tmp")
but failed.
However if I run the java command, it runs successfully.
r.exec("javac Test.java");
r.exec("java Test");
how should I do so that i can run the DOS commands metioned above in Java Environment?
thanks a lot.Have a look at http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html
This may help. I wonder if this is ok ?
Runtime r= Runtime.getRuntime();
r.exec("cmd.exe /C set classpath=%CLASSPATH%;.\\tmp\""); -
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).
-
Running ssh command from java and then answering password prompt
Hi,
I have a situation that has not solved yet. I am running ssh command from unix terminal without any problem, and then i enter password.
For example :
[oracle@fuata]:/export/home/oracle> ssh -N [email protected] -L 9901:127.0.0.1:9999
Password:
It is working. I have question that how can i perform this in java? I am thinking that i can run ssh command by using Runtime Class, it is ok. But how can i answer the password? I am a bit confused. Is there any example looks like this?
Thanks for responses.futi wrote:
Thanx. Firstly i insisted to do this without jsch but actually this is harder than jsch. I edit some of code pieces PortForwardingL.java and could run it. It works problem-free. Could you say why you "insisted" on this approach. It can't be for speed+ since jsch is very fast. It can't be for portability+ since jsch is portable but the use of Runtime.exec() requires the installation of ssh software. It can't be because of limitations+ since jsch is a fully featured library. It can't be for security+ since jsch is secure. It can't be for ease of use+ since jsch is much easier to use than ssh with Runtime.exec(). Unless it's a licensing issue, it can't be for commercial+ reasons since jsch is free. The only reason I can think of why one would "insisted" on this approach is if it is for some college project. -
How to run system commands from JAVA
Hi Friends,
How to run windows system commands from JAVA
Runtime r=Runtime.getRuntime();
r.exec("dir");
Throwing following Exception
CreateProcess :dir error=2
Thanks in advance
HamsaHi ,
in Windows NT this is not possible, you can use the following :
Runtime r=Runtime.getRuntime();
StringBuffer sbuf = new StringBuffer();
String dir = new String();
java.lang.Process proc = r.exec("cmd /c dir");
InputStream is = proc.getInputStream();
int ch ;
while((ch=is.read() ) != -1)
sbuf.append((char)ch);
is.close();
dir = sbuf.toString();
System.out.println(dir ); -
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
Maybe you are looking for
-
I am working in IND CS3 version 5.0.4 and Acrobat 9 Pro version 9.0.0 Computer: Windows XP Pro, Ver.2002, SP 3 Dell Optiplex GX 620, Pent. D 3GHZ, 3.50GB RAM Question: I am new to the PC environment and have noticed that everytime I double-click an I
-
Has anyone managed to connect an encrypted hard drive to airport extreme?
I am using Hitachi 4TB drive formatted to Apple Encrypted format, requiring password. The drive works fine on my MACs, when connecting it direclty via USB3 cable. However, the drive cannot be configured using Airport Utility. The Partion remains invi
-
Assign document type to sales area
Hi, I created a new document type and know when i assign this document type to sales area < 1000/10/10 >, its giving an error message " define < 1000/10/10 >first as general sales area" . I defined an sales area with that combination. can anyone help
-
WPA2 Enterpise + AES with wicd
Hi, I'm Matteo and I'm a student.... so to get access to internet i have to connect to a wireless connection that requires AES encryption but i dont know how to do that, i dont see this option when i configure the connection to WPA2 enterprise. So, I
-
ITunes library synchronisation across network
Hi everyone I am setting up my new Macbook Pro. I have imported all of my music files to ~/Music and all my movies to ~/Movies. I changed the iTunes folder organisation to manually. So far so good. Now I have bought an Apple TV and this Apple TV is r