Solved

java runtime  bat file run and close java program.

Posted on 2006-06-15
13
1,008 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
0
Comment
Question by:kmccusker
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 2
  • 2
  • +4
13 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 16915674
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/
0
 
LVL 92

Accepted Solution

by:
objects earned 250 total points
ID: 16916457
use the job scheduler to run the application, that way it won't exit and you can also control when it starts.
0
 
LVL 13

Assisted Solution

by:Webstorm
Webstorm earned 250 total points
ID: 16918918
try to use javaw instead of java to avoid console window that user can close.

instead of:
                    "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 +"\""};

use:
                    "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 +"\""};
0
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
LVL 13

Expert Comment

by:Webstorm
ID: 16918924
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 };
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 16920230
You can perhaps start it as a background process if you append & at the end of the command.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16920300
>>if you append & at the end of the command.

Only in *nix
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 16920352
As a matter of fact, I tried it in windows server 2003 enterprise edition and somehow it works~!
0
 
LVL 13

Expert Comment

by:Webstorm
ID: 16920422
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".
0
 
LVL 11

Expert Comment

by:DeanHorak
ID: 16920488
Simply use the Windows START command

i.e.

START background.cmd

or

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

0
 
LVL 4

Expert Comment

by:orhanbaba
ID: 16926651
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.
0
 
LVL 13

Expert Comment

by:Webstorm
ID: 16930466
Don't repeat what have already been said !
0
 

Author Comment

by:kmccusker
ID: 16933770
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
0
 
LVL 13

Expert Comment

by:Webstorm
ID: 16934788
:-)
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How to get all the API from website? 11 129
tomcat administrtor 12 68
servlet and mdb, jms error 1 57
Where to place postgres JDBC driver jar on tomcat 8 31
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question