How to execute a unix/dos command in Java
Hi,
I want to execute dos/unix commands in my java program. Can anyone tell me how to do this. Say I want to restart my httpd daemon using the command: "service httpd restart" or test my httpd.conf file using the command "testparn"
thanks in advance
Hugo Hendriks
hallo,
test this:
Process p = Runtime.getRuntime().exec(Your_Programm);
p.waitFor();
LineNumberReader lnr = new LineNumberReader(new InputStreamReader(p.getInputStream()));
Your_Programm must be a shellscript. regard that your java programm must have the right to start the httpd!
Carsten Bluetner
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 -
How to execute solaris lpd printing command from java code
hi folk,
I want to print a post script file on a network printer (my java code will receive the Printer's IP Address and the filename ) , i'm using the solaris lpd printing command :
lpadmin -p banana_ps -o protocol=bsd,dest=IP -v /dev/null
-m netstandard -T PS -I postscript
my question is how execute this command from the java code :)
i really appreciate ur advicesHi,
See RunTime.getRuntime ().exec (...).
Hope that help,
Jack -
How to execute DOS command in Java?
I want to execute a dos command in Java,such as execute test.bat command? How to realize it?
Thanks in advance!http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html
I found this article really useful in solving this. Hope it helps.
Cheers,
John -
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. -
Through a java prog how to execute a unix script ?
Hi !! I am a Java Developer . I have to work in Unix and Java in combination. Can you plz suggest me a solution for the below queries --
1. Through a java prog how to execute a unix script.
2. How to send the o/p of the script to a java prog so that it can be used in the java program to the prupose of display etc..Try this
Process process = Runtime.getRuntime().exec("ps -ef");//write Your command here
InputStream in = process.getInputStream();
BufferedReader bufReader = new BufferedReader(new InputStreamReader(in));
String data=null;
while((data = bufReader.readLine())!=null)
System.out.println(data); //do whatever you want to do with the output -
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 -
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\""); -
EXECUTE DOS COMMANDS WITH JAVA
I'm new to java and I want to execute dos commands by java
can someone help me by by anyway?
like tell me the packages or methods I need
or give me links to sites?
thanksNo Arguments:
try {
// Execute a command without arguments
String command = "ls";
Process child = Runtime.getRuntime().exec(command);
// Execute a command with an argument
command = "ls /tmp";
child = Runtime.getRuntime().exec(command);
} catch (IOException e) {
}With Arguments:
try {
// Execute a command with an argument that contains a space
String[] commands = new String[]{"grep", "hello world", "/tmp/f.txt"};
commands = new String[]{"grep", "hello world", "c:\\Documents and Settings\\f.txt"};
Process child = Runtime.getRuntime().exec(commands);
} catch (IOException e) {
} -
Execute dos commands through java
Hi,
Im trying to execute dos commands through java like
try {
java.lang.Runtime.getRuntime().exec("cmd /c cls");
}catch(IOException e){
System.out.println(e.getMessage());
}Not sure if its possible? however
open notepad would work
try {
java.lang.Runtime.getRuntime().exec("notepad");
}catch(IOException e){
System.out.println(e.getMessage());
}Im trying to execute a cls commands to clear screen but without luck.The question is, which shell do you want to clear?
I don't really know, but it could be that Runtime.exec executes its command in a new shell window... -
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, 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).
-
How can I run Runtime.exec command in Java To invoke several other javas?
Dear Friends:
I met a problem when I want to use a Java program to run other java processes by Runtime.exec command,
How can I run Runtime.exec command in Java To invoke several other java processes??
see code below,
I want to use HelloHappyCall to call both HappyHoliday.java and HellowWorld.java,
[1]. main program,
package abc;
import java.util.*;
import java.io.*;
class HelloHappyCall
public static void main(String[] args){
try
Runtime.getRuntime().exec("java -version");
Runtime.getRuntime().exec("cmd /c java HelloWorld "); // here start will create a new process..
System.out.println("Never mind abt the bach file execution...");
catch(Exception ex)
System.out.println("Exception occured: " + ex);
} [2]. sub 1 program
package abc;
import java.util.*;
import java.io.*;
class HelloWorld
public static void main(String[] args){
System.out.println("Hellow World");
} [3]. Sub 2 program:
package abc;
import java.util.*;
import java.io.*;
class HappyHoliday
public static void main(String[] args){
System.out.println("Happy Holiday!!");
} When I run, I got following:
Never mind abt the bach file execution...
I cannot see both Java version and Hellow World print, what is wrong??
I use eclipse3.2
Thanks a lot..sunnymanman wrote:
Thanks,
But How can I see both programs printout
"Happy Holiday"
and
"Hello World"
??First of all, you're not even calling the Happy Holiday one. If you want it to do something, you have to invoke it.
And by the way, in your comments, you mention that in one case, a new process is created. Actually, new processes are created each time you call Runtime.exec.
Anyway, if you want the output from the processes, you read it using getInputStream from the Process class. In fact, you really should read that stream anyway (read that URL I sent you to find out why).
If you want to print that output to the screen, then do so as you'd print anything to the screen.
in notepad HelloWorld.java, I can see it is opened,
but in Java, not.I have no idea what you're saying here. It's not relevant whether a source code file is opened in Notepad, when running a program. -
Hi
I have used google to get an answer for this.
This is the command i have used to run some dos commands in java
Unfortunately it doesn't throws any exceptions but still the directory and the file news.txt is not created.
Process t=objrun.exec("command.com /c dir > c:/news.txt");
Process p=objrun.exec("command.com /c mkdir c:/Prabhath");
Please help me....am using windows xp OS.Note that the java.util.File has the following methods that might do the job :public File[] listFiles()
public boolean mkdirs()
public boolean createNewFile()
Maybe you are looking for
-
<blockquote>Locking duplicate thread.<br> Please continue here: [[/questions/866032]]</blockquote> The problem is that EACH TIME I open Firefox 6.0 and now 6.1 I just installed it runs a compatibility ad-on check AND then opens the "what's new?" page
-
I have 2008R2 Remote Desktop Servers with Adobe Acrobat XI pro. When a new user tries to send a pdf via email, it pops up a message "How would you like to send this email" they have to pick "Default email application (Microsoft Outlook) and Remembe
-
Hi, my mac started itself to lose ethernet connection when i go away for 10 mins or more. When i come back it gets connection if i move the mouse only. I can't find where to set options or how to fix this issue. Can you help me please ? thx
-
Multiple connections in one BC4J Project
I want to access several Databases from one BC4J-Project. For example I want to write an application, which collects data from one database, does some computation and stores the results in a second database. I would prefer to use BC4J-Components for
-
I can't install Photoshop CS6 trial
Every time I try to install the Photoshop CS6 trial, i'm greeted with this message - This application cannot be installed because this installer has been mis-configured. Please contact the application author for assistance. What do I do?