Solved

java runtime  bat file run and close java program.

Posted on 2006-06-15
13
1,004 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
  • 5
  • 2
  • 2
  • +4
13 Comments
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 13

Expert Comment

by:Webstorm
Comment Utility
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:mayankeagle
Comment Utility
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
Comment Utility
>>if you append & at the end of the command.

Only in *nix
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 30

Expert Comment

by:mayankeagle
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Don't repeat what have already been said !
0
 

Author Comment

by:kmccusker
Comment Utility
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
Comment Utility
:-)
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
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…

771 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now