Win a copy of TDD for a Shopping Website LiveProject this week in the Testing forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Launching java application from batch file

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 22644
123
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
terr lundamo
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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?

 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Sheriff
Posts: 3837
66
Netbeans IDE Oracle Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 Vashko
Sheriff
Posts: 3837
66
Netbeans IDE Oracle Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try to replace that code with this:

Let's see whether it changes anything.
 
terr lundamo
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 Vashko
Sheriff
Posts: 3837
66
Netbeans IDE Oracle Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Any sufficiently advanced technology will be used as a cat toy. And this tiny ad contains a very small cat:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic