How to run sequential commands by runtime or whatever in java?
I'm doing a compile function in java GUI and need to run several commands one by one, how can i do this and how can i know that the previous command has been done and finished?
commands like
make ....
make ....
make ....
then do
./run.x ....
thanks a lot!
THe Process object that Runtime.exec method returns has a method called waitFor. Calling it makes the currently executing Java thread wait for the process to exit.
(apidoc: http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Process.html#waitFor())
Similar Messages
-
How to run media file after Runtime.exc the real player?
Hi
How to run media file after Runtime.exc the real player applicationString command = "cmd /c start videotest.rm";
Runtime.getRuntime().exec(command); -
How do run unix command in java
hi
All unix command working fine in our java program.
but i want change user in linux by using java. it's not working.
"su root" This command onely not working.
anybody know help me
This is my ID [email protected]
This my code
<% String s = null;
try{
Process p = Runtime.getRuntime().exec("su root");
BufferedReader stdInput = new BufferedReader(new
InputStreamReader(p.getInputStream()));
BufferedReader stdError = new BufferedReader(new
InputStreamReader(p.getErrorStream()));
out.println("Here is the standard output of the command:\n");
while ((s = stdInput.readLine()) != null)
out.println(s);
catch(Exception e) {}
%>I don't have further info to add to your first post, but think your guess seems quite reasonable. I thought of starting a new thread on the same topic, but think this (question) fits in here, too.
I have Java code (freeware, not opensoure), intended for Unix, which basically provides the graphical interface and relies on an I/O layer (C + shell scripts) to do most of the work. I have access to the C+shell scripts which are opensource. I ported it to Cygwin and want to use it under Windows (there is no Cygwin native Java VM).
My problem now boils down to " how to run commands under Unix and what differences are there with Windows". What are the variants?
I have to guess what the program is doing when I get an IOException at some point. E.g.: a call to shell script may be made in a different way as to a compiled exe in Unix?. I found a way to bypass path problems because forward slashes are also accepted and /cygdrive/c construct can be replace by c:/; shell scripts can be compiled into exes using shc and they work; if symbolic links are replaced by duplication of exe files, they work. What will happen in an instance whereby a process runs a shell script dynamically, through a pipe (the shell script is compiled in the Cygwin/win version and receives parameters) in a construct like:
pipefp = epopen(cmdbuf,"w"); /* (cmbuf is "makehdr par1 par2 ... ") (makehdr was a shell script and is now compiled exe for Cygwin/Windows) */?.
Thanks.
LT -
Please help how to run System commands and batch files thru java program
Sir,
I want execute OS commands like dir,cls..etc and batch files,.exe filesthru java program.
I have tried it sofar thru Runtime.getRuntime().exec("execute.bat");
but it is not working.anybody knows about how to run the system commands thru java please give me the code.
thank you,
regards,
j.mouliI've seen other posts with this questions, and answers. Unfortunately I didn't pay much attention. But the basic idea is to exec command.com and pass the specifc batch or command as an argument.
-
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\""); -
How to run Unix command with Pipe ( | )?
Dear Friends,
I have to execute the below unix command through java program.
*ps -ewwo pid,args | grep -E "abc.sh|xyz.jar" | gawk '{ print $1 }' | wc -l*
My code to execute this command is,
Runtime run = Runtime.getRuntime();
File workDir = new File("/root/sat");
String psCmd = "ps -ewwo pid,args | grep -E \"abc.sh|xyz.jar \" | gawk '{ print $1 }' | wc -l";
Process process = run.exec(psCmd, null, workDir);
String line;
int i = process.waitFor() ;
if(i == 0) {
BufferedReader buf = new BufferedReader( new InputStreamReader( process.getInputStream() ) ) ;
while ( ( line = buf.readLine() ) != null )
System.out.println("Line - ["+line+"]") ;
} else {
BufferedReader stdErr = new BufferedReader(new InputStreamReader(process.getErrorStream()));
// read the output from the command
while ((line = stdErr.readLine()) != null) {
System.out.println(line);
}When i execute this command, i'm getting output as,
ERROR: Garbage option.
When i analyse the above error, i found that, the PIPE ( | ) command is not supported through java.
Could anyone please help me how to execute the above unix command (with | ) in java?
Thanks in advance,
SathishThe pipe has to be interpreted by a shell so you need
String[] psCmd =
"sh",
"-c"
"ps -ewwo pid,args | grep -E \"abc.sh|xyz.jar \" | gawk '{ print $1 }' | wc -l"
Process process = run.exec(psCmd, null, workDir);You should also read, re-read and then implement the recommendations in the 4 sections of http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html . -
How to run a command line argument
i want to run a command line argument, say for eg......i want to execute the "dir" command on the dos prompt and capture the output.
how can i do this.
actually i want to capture the output of the ping command and write it to a file. can any body plsssssss helpThis command runs a ping to "address" and writes the output back to the command line. To capture it, just put strings into an array or do whatever you want with it. If you want to wait until the program completes before reading its input, use p.waitFor().
Process p = Runtime.getRuntime().exec("ping " + address);
BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
String currLine = null;
while((currLine = in.readLine()) != null)
System.out.println("ping: " + currLine);
[/code -
Running interactive command with Runtime.exec
I'm trying to run a command via the Runtime.exec interface.
Occasionally, the command needs to prompt for additional information. The response depends on the specific configuration, however, the command returns a list of options and then waits for a response.
However, when the command waits for the response, my Java app hangs.
After I call Runtime.exec, I create 2 threads to consume the contents of stderr and stdout. I then start them and call proc.waitFor()
I would expect to see the output of the command in the stdout stream even though the command hasn't exited. I had hoped to parse the output to determine the necessary response. However, the calls to read the contents of the stdout and stderr streams block and I never see any output.
How can I get access to the contents of those streams while the command is still running? Is this supported through the Runtime.exec interface?
Thanks,
ShawnThis article should help:
http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html -
How to run solaris commands through java code ....
Hi,
actually i want to run some solaris commands for zipping some files on Solaris OS...
any idea how can i do that ?
thankspublic class TABLES
public static void main( String[] args )
//database is connected
try
Connection con = null;
Statement stmt = null;
String strShowTables = "";
ResultSet resultSet = null;
// CBA Statistics period is m_lStatisticsPeriod minutes
con = DriverManager.getConnection( g_strRWURI, g_strRWUsername, g_strRWPassword );
stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("use db");
resultSet = stmt.executeQuery(strShowTables);
String tableName = "";
while(resultSet.next()){
tableName = resultSet.getString(1);
System.out.println(tableName);
break;
String strCmd = "tar cvzf file.tar.gz var/lib/mysql/db/GROUPS.*";
Process p= Runtime.getRuntime().exec(strCmd);
System.out.println(strCmd);
stmt.close();
rs.close();
resultSet.close();
con.close();
catch ( Exception e )
System.out.println( ": Failed to create database connection (" + e.getMessage() + ")" );
e.printStackTrace();
catch ( Throwable t )
System.out.println( " Throwable: " + t.getMessage() );
t.printStackTrace();
}//end of main mehtod
}//END OF CLASSi hava tried the above code... what the problem is
when is run that command on shell > tar cvzf file.tar.gz var/lib/mysql/db/GROUPS.*i works fine but in code even though it didn't give any error but the created "file.tar.gz" is empty...
Edited by: aftab_ali on Apr 7, 2009 7:15 AM
Edited by: aftab_ali on Apr 7, 2009 7:17 AM -
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 ); -
How to run a command line window from code?
In an application I need to run a dll file with a few arguements. These arguements can be given in the command line window. The execution of the dll file will return a string that i need to get (for the parsing etc).
1. How can I open a command line window from the Java code?
2. How to run commands on it?
3. How to get the results of the commands from the command line into my program?In an application I need to run a dll file with a few
arguements. These arguements can be given in the
command line window. The execution of the dll file
will return a string that i need to get (for the
parsing etc).
A dll is used by other programs. It is not possible to run it from the command line. -
How to run zonecfg command-line to add device in postinstall script?
I want to assign a tape drive in non-global zone in postinstall script. I can run zonecfg with a command-file, but don't know how to run zonecfg to add device in a whole command-line.
The command-line fails to run:
# zonecfg -z sun1 add device set match=/dev/rmt/173b
syntax error at 'set'
Commands:
add <resource-type>
(global scope)
add <property-name> <property-value>
(resource scope)
cancel
commit
create [-F] [ -b | -t <template> ]
delete [-F]
end
exit [-F]
export [-f output-file]
help [commands] [syntax] [usage] [<command-name>]
info [<resource-type> [property-name=property-value]*]
remove <resource-type> { <property-name>=<property-value> }
(global scope)
remove <property-name>=<property-value>
(resource scope)
revert [-F]
select <resource-type> { <property-name>=<property-value> }
set <property-name>=<property-value>
verify
Thanks for your help!Quote the parameters and embed the newlines:
zonecfg -z sun1 'add device
set match=/dev/rmt/0
end' -
How to run a command prompt " command " through java code
hi all,
There is a command
"java -jar selenium-server.jar -interactive"
which i am running through command prompt after going to D:\MyFolder\Examples .
i want to execute this command using java code .please helpThis has already been answered in your other two threads on this topic - http://forum.java.sun.com/thread.jspa?threadID=5221221&messageID=9898287#9898287 and http://forum.java.sun.com/thread.jspa?threadID=5221223&messageID=9898290#9898290.
For some reason you don't want to read the reference that tells you exactly how to do what you want and how to avoid the pitfalls - http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html . -
How to run unix command from Oracle Foms 10g
OS: UNIX Solaries, Oracle Application Server 10g
To run shell script from Oracle Forms, I used the following host('/bin/bash /u01/compile.sh') and it works well
Now, I need to run unix command something like
host('mv form1.fmx FORM1.FMX') but it's not working
I tried to append the command "mv form1.fmx FORM1.FMX" to the compile.sh shell script but also it's not working although the rest lines of the shell script is running well
Edited by: slamonty on Aug 23, 2012 12:36 AMYes, Thank you so much, it works well as follow
$ /bin/mv /u01/oracle/runtime/test/form1.fmx /u01/oracle/runtime/test/FORM1.FMX Edited by: slamonty on Aug 23, 2012 9:59 AM
Edited by: slamonty on Aug 23, 2012 11:14 AM -
hi all,
i am running this command at command prompt
like below
java filename argument.
ex: java validateuser user1
it works fine at command prompt. and the same command i want to run in jsp
so i tried like this
Runtime x = Runtime.getRuntime().exec("java validateuser user1");
whats happenning here is it just opening command promot of java.exe but its not running validateuser.
any help will be greatly appreciated.
advance thanks.why on earth are you trying to run java.exe on the command line through JSP?i think you misunderstand my question.
i am not trying to run java.exe on through jsp.
I am trying to run a validateuser user1 in jsp .
Note:validateuser is a java file which take one argument.here in my scnerio user1 is the argument.
Maybe you are looking for
-
I used to be able to minipulate huge photoshop files. Now I can't even get one to go from 20 to 50 gig! All I get is this out of ram error. I have a 400G scratch disk and well over optimum on all settings! NOT HAPPY!!
-
Tables - How to get cell value? How to get/set UI controls properties?
Hi, I want to get a the cell's value of row x and col y. The table is not bounded so I cannot use: Table1.Items(key).DataSourceRow.DataItem("ColID") Another question: How to I set the properties of a table column which contains UI elemtents that I cr
-
Seeking a Guru to count number of tasks (apple script).
I came across a post that describes how to get a count of events in ical, and am wondering if there is a Guru who knows how to get the total for the number of reminders (tasks)? This is the code to get the events. set myText to "Count of events in i
-
Hi all, I am new to webdynpro java. I am trying to understand the concept of copmonent reuse. I have a doubt in the following development which i am trying now. Consider that i have two components in my development project. Let the name of the develo
-
Mac Multimedia Transfer app not working with N95 8...
I'm using the latest 1.2 version of the Multimedia Transfer app, but it does not recognize the phone via bluetooth nor USB. When trying to setup a device in the Multimedia Transfer app I see my phone listed in the device list, but it's grayed-out and