We help IT Professionals succeed at work.

java runtime  bat file run and close java program.

kmccusker
kmccusker asked
on
1,058 Views
Last Modified: 2012-05-05
Hello experts!

The code snippet below runs a bat file that executes for about six hours.  I am on a windows 2003 operating system.  I would like to be able to have the user submit the job then go home.  In the morning an e-mail message tells them the job has completed, success or failure.  I have everthing working except when the user closes the window that executed the code snippet below, the backround process ends.  Is there a way in JAVA on a windows 2003 operating system to submit a background process that is not a child process.  Any code or pointers to documentation would be great.


                runtime = Runtime.getRuntime();
                String[] cmd = {"cmd",
                    "/c", "start",
                    "java -cp E:\\Winapps\\TrialBalance;E:\\Winapps\\TrialBalance\\classes;c:\\java\\javamail\\javamail-1.4ea\\mail.jar;c:\\java\\javaactivation\\jaf-1.0.2\\activation.jar Main \"false\" \"ALL\" \"" + fy +"\""};
                p = runtime.exec(cmd);
                af.writeLogEntry("Submitted process");
                af.writeLogEntry("Command -> E:\\Winapps\\TrialBalance;E:\\Winapps\\TrialBalance\\classes;c:\\java\\javamail\\javamail-1.4ea\\mail.jar;c:\\java\\javaactivation\\jaf-1.0.2\\activation.jar Main \"false\" \"ALL\" \"" + fy +"\"");

Thanks

Kevin
Comment
Watch Question

CERTIFIED EXPERT
Top Expert 2016

Commented:
AFAIK the only way to have a 'background' process that can't be shut down easily is to use a Windows service. See

http://wrapper.tanukisoftware.org/
Java Developer
CERTIFIED EXPERT
Top Expert 2010
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
Top Expert 2004
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
Top Expert 2004

Commented:
You can also avoid cmd :

                String[] cmd = {"javaw","-cp",
 "E:\\Winapps\\TrialBalance;E:\\Winapps\\TrialBalance\\classes;c:\\java\\javamail\\javamail-1.4ea\\mail.jar;c:\\java\\javaactivation\\jaf-1.0.2\\activation.jar",
                 "Main","false","ALL", fy };
Mayank SPrincipal Technologist
CERTIFIED EXPERT

Commented:
You can perhaps start it as a background process if you append & at the end of the command.
CERTIFIED EXPERT
Top Expert 2016

Commented:
>>if you append & at the end of the command.

Only in *nix
Mayank SPrincipal Technologist
CERTIFIED EXPERT

Commented:
As a matter of fact, I tried it in windows server 2003 enterprise edition and somehow it works~!
Top Expert 2004

Commented:
On Windows shell (DOS), & is used to call multiple commands :   cmd1... & cmd2...
(equivalent of ; on Unix/Linux)

&& and || can be used on Windows & Unix/Linux.
but there is no other equivalent of & (Unix/Linux) in Windows than "start".
Simply use the Windows START command

i.e.

START background.cmd

or

START /B background.cmd               if you don't want a window

you can use start

start /B /SEPARATE commands

if you have multipler process. like dir rmdir mkdir... you  can use it like that.
start /B /SEPARATE dir
start /B /SEPARATE mkdir meeek
:)
each process start at the samte time.
Top Expert 2004

Commented:
Don't repeat what have already been said !

Author

Commented:
Wow!

  I just wanted to say thank you.  I split the points between webstorm and objects.  Webstorm pointed out the problems I needed to resolve with my java program.  However, objects pointed out on windows 2003 I already have a program, the scheduler, that will do the task for my user.  Why re-invent the wheel unless you want to! :)  Again, I thank you for the quick response and the great advise.

Kevin
Top Expert 2004

Commented:
:-)
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.