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
Similar Messages
-
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 -
Executing a dos command from java
Hi guys.
This line works fine:
process = Runtime.getRuntime().exec("command.com /C c:\\test.bat");
But this line do nothing:
process = Runtime.getRuntime().exec("command.com /C c:\\test\\test.bat");
I need to run a dos file in some directory in my HD. It works fine in root, but do nothing when I change to directory. I do this from an applet.
Any ideas/suggestions?Any ideas/suggestions?The file isn't actually there (it isn't in the directory.)
There is a permisions problem (which is unusual but not impossible on windows.)
The script itself is failing - you didn't indicate "how" it doesn't work so this is possible. -
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.). -
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 -
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 all,
Is it possible to execute a DOS command in java? If so, how do I do that?
Also, if it is a Linux machine, may I execute a Linux command in java?
Thanks for your help.
Regards,
BernardFirst, define 'command'.
You can run external programs with Runtime.exec (eg. Runtime.getRuntime().exec("notepad");) but there are a few drawbacks regarding that method.
http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html -
Can I execute MySql's command from java application?
Can I execute MySql's command from java application? And how?
For example :
load data local infile 'D:\\myData.txt'
into table myTable
fields terminated by ';'
lines terminated by '\n';1. get the jdbc driver for mysql from the mysql site at: http://dev.mysql.com/downloads/connector/j/5.0.html
2. follow the installation instructiions... which you'll also find in your mysql manual...
Happy travels. Keith. -
Executing a DOS Command from JSP
How do i execute a Dos command from within JSP. I would like to execute a sql Loader command .
ThanksYou use Runtime.execute().
Take a look here for a few of the tricks involved in it:
http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html -
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. -
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()?) -
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? -
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 -
Executing a bash command from Java
I want to execute a bash command from within a Java application on the server. I know how to do simple commands, but this command is killing me. I get nothing back when I send it.
p = Runtime.getRuntime().exec(new String[]{"ps","-ef","","grep","evolution"});
There is also another part where I want the results piped to awk and fetch out the pids. BUt with this not working the rest is a waste for now.
Robbieevgeni00 wrote:
Thank you for assuming the posters' stupidity.I can only see the information that you provide. You didn't say you tried the recommendations.
I read the article and I tried the things he/she is proposing, also the error stream, guess what it outputs: nothing.Maybe you did it wrong. If you didn't do it wrong, then that program you're invoking is poorly written, because it dies in error without producing any error messages.
And I also searched for the exitValue 138, but didn't get much further than that its some system specific value. If you know where I can find more information, I would appreciate if you let me know about it. As jschell pointed out it's probably app specific. You can search the source code and/or documentation of that program to see if they say what 138 means.
Apart from that -- it seems that environment and run directory issues can trip this up. Try checking that stuff out. Maybe your program expects something on the executable PATH that isn't there. Or maybe you're not running it in the working directory that you think you are.
Maybe you are looking for
-
IPod versus iTunes, organizing music
I spent 2 hours last night organizing my music that had featuring artists so it would show up under the album it belongs to, versus being its own separate "album"... so I assumed that if iTunes was perfectly organized and the music is listed as it sh
-
I have made a form in acrobat pro and i want use this form on the Ipad. But on the Ipad with acrobat reader save the document automatically and i want save all the versions and not finally one document..Can somebody tell me this is possible or not?!
-
hi. i have a torch 9810 smartphone thats not acting so smart.. :-) or could be me. well.. theres only 512mb memory for download apps to, only downloaded 4 and its full. how do i change the settings to use a media card to fit more apps onto.. thanks
-
Hi, I am doing a search on a table using a date column having index defined on it. when I am using below query:[Here 26-OCT-2010 is the current date.] SELECT * FROM cs_incidents_all_b WHERE incident_date BETWEEN TO_DATE ('25-OCT-2010 00:00:00', 'DD-M
-
What does the extension "1.0" do. Mal-ware or part of FF7?
I recently noticed that a new extension appeared in the add-on lists: It's listed as "1.0"-- no other identification present. I tried searching the available add-ons and ended up with hundreds of responses. Is it mal-ware or part of FF7?