aspose file tools*
The moose likes Java in General and the fly likes Launching java application from batch file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Launching java application from batch file" Watch "Launching java application from batch file" New topic
Author

Launching java application from batch file

terr lundamo
Greenhorn

Joined: Dec 08, 2010
Posts: 19
Hi all,

I've made an application that i distribute, when the application is launched i first launch a jar file that checks for updates.
When this is done it runs a batch file that launches the main application (sometimes newly updated by the updater).

All this is fine, but some times the main application does not show, it just runs in the background. I can see it's running in task manager (i'm on windows), and uses the same amount of memory as a normal run.
If i open a second instance of the application, the one stuck in the background will also show, so then i have to applications running and showing...

Any tips on how to fix this, so it never get "stuck" running in the background?
terr lundamo
Greenhorn

Joined: Dec 08, 2010
Posts: 19
btw, here is everything that's in the bat file:


Maybe there is better command than "java" that forces application to the foreground? can't find anything useful on google. Have tested it on several computers and same problem everywhere, seems to be random when it runs in background.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19556
    
  16

First of all, on Windows you have to use \ instead of / inside batch files and other commands. Java is lenient with java.io.File but Windows itself isn't.

If it's a windowed application you may want to use javaw.exe instead of java.exe. This will not show the command window.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
terr lundamo
Greenhorn

Joined: Dec 08, 2010
Posts: 19
Rob Spoor wrote:First of all, on Windows you have to use \ instead of / inside batch files and other commands. Java is lenient with java.io.File but Windows itself isn't.

If it's a windowed application you may want to use javaw.exe instead of java.exe. This will not show the command window.


Thanks

Ok, i changed to "\" and use javaw instead of java.
Actually I never got a command window from running java.exe, maybe it's because i run the bat file from updater.jar. If i run the bat file manually i get a command window.
Anyway, it's the same problem with javaw. The second time i ran the updater which launches the bat file, the main application didn't run in foreground.

Maybe i'm looking at this the wrong way.. The reason i use the bat file is because when i launch the main application from the updater, i can never close the updater since the main application is a child thread, and this would close both the updater and main application.
Is it a better way to do this perhaps?

Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Rob Spoor wrote:First of all, on Windows you have to use \ instead of / inside batch files and other commands. Java is lenient with java.io.File but Windows itself isn't.


It's worse than that. It's inconsistent--lenient in some cases and not in others.

You can use / in the command, such as C:/Windows/putty.exe, but you have to use \ in the argument. So, for instance, dir C:/Users/JJV won't work; we have to use dir C:\Users\JJV. I guess this is because / is still an option indicator like - is in *n*x. So we can have c**k-ups like C:/some_dir/some_command C:\some_dir\some_file_arg.

One of many reasons why one of the first things I do when I get a new Windows box is install cygwin.
Martin Vajsar
Sheriff

Joined: Aug 22, 2010
Posts: 3454
    
  47

Maybe you could show us how do you run the bat file (I mean the code you use in updater.jar)? I'd say that you should be able to get rid of the bat file and run java (or javaw) directly from your updater application. It might help resolve the issue.

I also understand that the window of your application is not visible anywhere: taskbar, Alt-Tab list, Task Manager Applications tab (just in processes). Is this the case?

Also, I assume your main application is a Swing app. Do you initialize all Swing objects on an Even Dispatch Thread (using SwingUtilities.invokeLater and all that)? It kind of seems that the window of your application exists, but is hidden.
Rod Singh
Greenhorn

Joined: Feb 10, 2012
Posts: 16
With regards to Windows Task Manager, your .bat script -- which invokes java -- hides the java process. If you would want to explicitly list the java process visible in your process list -- run it explicitly as java -jar "C:/app/app.jar" from the command shell.
terr lundamo
Greenhorn

Joined: Dec 08, 2010
Posts: 19
Martin Vajsar wrote:Maybe you could show us how do you run the bat file (I mean the code you use in updater.jar)? I'd say that you should be able to get rid of the bat file and run java (or javaw) directly from your updater application. It might help resolve the issue.

I also understand that the window of your application is not visible anywhere: taskbar, Alt-Tab list, Task Manager Applications tab (just in processes). Is this the case?

Also, I assume your main application is a Swing app. Do you initialize all Swing objects on an Even Dispatch Thread (using SwingUtilities.invokeLater and all that)? It kind of seems that the window of your application exists, but is hidden.


This is how i run the bat file from updater.jar

I don't remember why i had to use double "\\", but there has never been a problem launching the bat file


Sorry if i was unclear, i can see the application running in task manager applications as a process (java.exe or javaw.exe when i tried javaw insted of java).
Yes it's a swing application, nothing fancy about it really(event dispatch thread).
If i start the main application directly it always works

What makes it so strange is if i run the updater a second time (with the main app still just running in the background) it always works, also the main application "stuck" in the background also comes to the foreground, and then i have two visible windows of the main application. So the second run of the updater.jar makes the old and new main.app show.
Martin Vajsar
Sheriff

Joined: Aug 22, 2010
Posts: 3454
    
  47

Try to replace that code with this:

Let's see whether it changes anything.
terr lundamo
Greenhorn

Joined: Dec 08, 2010
Posts: 19
Martin Vajsar wrote:Try to replace that code with this:

Let's see whether it changes anything.


When i try this it's the same result, except i see a process javaw.exe in task manager. Everything behaves the same.
Could it have anything to do with whether updater.jar manages to close itself before the main app starts up?
Martin Vajsar
Sheriff

Joined: Aug 22, 2010
Posts: 3454
    
  47

terr lundamo wrote:Could it have anything to do with whether updater.jar manages to close itself before the main app starts up?

In Windows, there are some convoluted rules concerning application windows after launch, but I believe they govern focus, not visibility. However, if your updater application quits immediately after starting up the main app, it might influence things. Delaying the exit by a few seconds long sleep() after that just to see what happens cannot hurt.

When you run the application from command line, does it show up nicely? Does it behave the same on all computers?
terr lundamo
Greenhorn

Joined: Dec 08, 2010
Posts: 19
Martin Vajsar wrote:In Windows, there are some convoluted rules concerning application windows after launch, but I believe they govern focus, not visibility. However, if your updater application quits immediately after starting up the main app, it might influence things. Delaying the exit by a few seconds long sleep() after that just to see what happens cannot hurt.

When you run the application from command line, does it show up nicely? Does it behave the same on all computers?


I'll try delaying first thing tomorrow, i would think something like that is the problem/solution since it seems random when the problem occurs.
When i run the application from the command line it always shows up nicely on every computer, so there has to be some issue with the updater in combination with the bat file.

Thanks
terr lundamo
Greenhorn

Joined: Dec 08, 2010
Posts: 19
a little crazy day at work, so didn't get the chance to test with delay, will post results when i find the time
terr lundamo
Greenhorn

Joined: Dec 08, 2010
Posts: 19
Sorry for late reply, been sick all week.

anyway, i've tested with a 4 sec delay before closing the updater, and it works like a charm.

Thanks!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Launching java application from batch file
 
Similar Threads
Tomcat Service VS batch file
Exiting a multi-threaded Swing application without System.exit
How to check if application is running?
How to get both ErrorStream and InputStream of a Process
How to unload a java class from memory?