Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Runtime.exec()

 
Kishan Kumar
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I want the process iniated by Runtime.exec() to be executed as a daemon process. Is there any way of doing it. When I execute a java program inside exec I do not want the cmd console to come up. Is there any work around for this.

TIA,
 
Rob Ross
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You don't have control over the Process; it's not a JVM thread, it's an OS-Native process.
However, when you invoked commands via exec() you can pass the same parameters as you would normally on the command line to get the effect you want.
Are you actually sending the command "java.exe" to the exec() method? How about using javaw instead? That won't open up a window. There are also command switches you can use, you'll have to type help cmd at a dos prompt to see them and experiment a bit.
Exactly what are you trying to do, and what is happening, and what is not working the way you want?
 
Kishan Kumar
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ross,
Thanks for your reply...My requirement is that Iam developing a java based scheduler....what should be executed is placed in the database.
Iam just thinking on what form of the executable should i have. All are java classes. Iam thinking of wrapping it inside a .bat file so that .. take from the database...build the Task object and place it inside the scheduler. Since what is to be executed is generated dynamically from database I want to have objects instaintiated from one class having a paramter what to execute.
I went through java.lang.relfect where I can execute java classes dynamically...it needs a bit of experimentation .. since time is a constraint I just wanted to use RunTime.exec("execute.bat") which will have (java classname) inside it.
But as I happen to see it opens console each time a .bat is executed..But scheduler is a background process. Hope my requirement is clear...I will workout on the solutions provided by you previously. Thanks for your time and effort.
 
Kishan Kumar
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ross,
Thanks for your reply...My requirement is that Iam developing a java based scheduler....what should be executed is placed in the database.
Iam just thinking on what form of the executable should i have. All are java classes. Iam thinking of wrapping it inside a .bat file so that .. take from the database...build the Task object and place it inside the scheduler. Since what is to be executed is generated dynamically from database I want to have objects instaintiated from one class having a paramter what to execute.
I went through java.lang.relfect where I can execute java classes dynamically...it needs a bit of experimentation .. since time is a constraint I just wanted to use RunTime.exec("execute.bat") which will have (java classname) inside it.
But as I happen to see it opens console each time a .bat is executed..But scheduler is a background process. Hope my requirement is clear...I will workout on the solutions provided by you previously. Thanks for your time and effort.
 
Rob Ross
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When I try this, I get a new java program that launches but it doesn't open up a new window


my runApp.bat batchfile looks like this:

When I run RuntimeTest from a command prompt I type:
java RuntimeTest
My runApp.bat file is executed. (Note I have both the java app and the batch file in the same directory for this test.) But no new command window is being opened.
How is this different that what you are doing?
 
Roseanne Zhang
Ranch Hand
Posts: 1953
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
cmd /c start /B whateveryouwanttoexecute
This will not open a dos window and execute whateveryouwanttoexecute on the background
 
Sandeep Lakshmipathy
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear friends,
If i am running a GUI based java app , and when i do "cmd \c start \B somecmd" , I will get a console window and the command is executed. Note that I am using an IDE and running my program from it. Is there a way to avoid this?
Thanks
 
Roseanne Zhang
Ranch Hand
Posts: 1953
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You used 2 wrong slashes.
 
prabhu pandurangan
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Members,

Your posts was good and it helped me a lot.

I tried to execute:

Runtime.getRuntime().exec("cmd /c start mysql -u username -ppass databasename");

for me it is connected to my mysql and the mysql command prompt opens.

But in my case instead of opening command prompt, i have to run my mysql restore command:
"mysql -u username -ppass databasename < c:/backup.sql";

But this is not working, im unable to restore database through this command.

Please help me to solve this issue.

Regards,
Prabhu.
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
prabhu,
do you realize that you responded to a post from 2002? None of the folks are likely to be around any more. Please DontWakeTheZombies.

You may want to read this article to learn about what can go wrong using Runtime.exec.
 
prabhu pandurangan
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ulf,

Sorry as im trying this for past 1 week, im very much eager to find the solution for this. However i read this article, but not clear with that.
Thats why i replied to the older post.

I think you may be familiar with my post... can you please help me....
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by prabhu rangan:
But this is not working, im unable to restore database through this command.


Can you expand on this a bit. Do you get any error messages from the mysql command ?
 
prabhu pandurangan
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joanne,

Im not getting any error message. What i need is i have to restore my database backup, through Runtime.exec()?

So, i tried by this way:

exec("mysql -u username -ppass databasename < c:/backup.sql");
(application executes without any error, but my database is not restored.)

so i tried this way:
exec("cmd /c start mysql -u username -ppass databasename");
(this time i got connected to my mysql command prompt and the black screen poped up...)

then in mysql command prompt, if i give the command "source c:/backup.sql"
my database gets restored.

But i need to restore my database for the first time without poping up the mysql command prompt.

Hope you are clear with my post...

Everyone gave me the http://www.javaworld.com/cgi-bin/mailto/x_java.cgi?pagetosend=%2Fexport%2Fhome%2Fhttpd%2Fjavaworld%2Fjavaworld%2Fjw-12-2000%2Fjw-1229-traps.html
link and asked me to read...

but i gone through this... but i could not find the solution...

Please help me out of this...
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by prabhu rangan:
So, i tried by this way:

exec("mysql -u username -ppass databasename < c:/backup.sql");
(application executes without any error, but my database is not restored.)


Forget Java for the moment. If you open a command prompt and type this command
mysql -u username -ppass databasename < c:/backup.sql
does your database get backed up correctly ?
 
prabhu pandurangan
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes it is working fine.
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Everyone gave me the http://www.javaworld.com/cgi-bin/mailto/x_java.cgi?pagetosend=%2Fexport%2Fhome%2Fhttpd%2Fjavaworld%2Fjavaworld%2Fjw-12-2000%2Fjw-1229-traps.html
link and asked me to read...

but i gone through this... but i could not find the solution...

Does that mean your code is handling the various streams correctly?

Also, you may need to use an absolute path for the "mysql" executable.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by prabhu rangan:
yes it is working fine.


Was anything output to the command window ?
 
prabhu pandurangan
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@joanne: No, nothing i got output.

@ulf: To restore the database should i need to handle streams. I dont need any output, just i need my command to execute thats it.
In case of taking backup of database i need to handle streams properly.
Backup process is working fine for me, i have problem only in restoring the backed-up database to mysql, using the java runtime.

Im struggling to find a solution to this issue.

Regards,
Prabhu.
 
vipin jos
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Please check the following code. Its a bit crude. Some code review and beautification can be done.

Run your command in this and check what is the output you are getting. What this does is - runs the command as a process, gets the output(The Thread.sleep and wait count is used to wait and read till the process completes.

import java.io.*;
import java.util.*;
public class RuntimeTest {
public static void main(String args[]) {
try {
byte[] bytes = new byte[2048];
Process session = Runtime.getRuntime().exec("cmd /c dir /p");
InputStream in = session.getInputStream();

int waitCount = 0;
while(waitCount++< 10) {
int read = in.read(bytes);
if(read != -1) {
String curString = new String(bytes,0,read);
System.out.println(curString);
}
Thread.sleep(500);
}
} catch(Exception e) {
e.printStackTrace();
}

}
}

This is not the solution to your problem, but can you please post what is the output of this code.


The output of my code looks like this(the dir command output)

Volume in drive D is Applns
Volume Serial Number is FC82-3B4A


Directory of D:\xx\brain\javatrial\new

11/19/2008 12:15 PM <DIR> .
11/19/2008 12:15 PM <DIR> ..
11/18/2008 02:13 PM 555 PowerTest.class
11/18/2008 02:12 PM 239 PowerTest.java
11/19/2008 12:10 PM 1,077 RuntimeTest.class
11/19/2008 12:10 PM 566 RuntimeTest.java
11/19/2008 12:07 PM 112 tp1c3b60.BAT
11/19/2008 12:08 PM 112 tp1c3cce.BAT
11/19/2008 12:10 PM 112 tp1c40d8.BAT

11/19/2008 12:10 PM 112 tp1c41dd.BAT
11/19/2008 12:15 PM 112 tp1c4d6e.BAT
9 File(s) 2,997 bytes
2 Dir(s) 17,091,596,288 bytes free

Press any key to continue . . .




Hope this helps.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic