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
Solved

java runtime  bat file run and close java program.

Posted on 2006-06-15
13
1,007 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
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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
arguments to jar 5 33
String array comparison 4 38
hashmap order 17 36
restrict decimal places for double datatype 10 19
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
The viewer will learn how to implement Singleton Design Pattern in Java.

809 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