Win a copy of Murach's MySQL this week in the JDBC and Relational Databases 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

"Hello world" not printing from executable jar

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I created the prototypical "Hello World" program in Java as "Test.java"

it compile just fine.

It runs fine when I do:
java Test

I jar'd the class with a manifest file that set the main class.

That runs fine if I do:
java -jar Test.jar

HOWEVER, if I just try to execute the Jar file either from the command prompt or by double clicking on the file then, I don't get any errors but it just doesn't do anything. What am I doing wrong?

By the way, I tried creating an application that had a GUI with a frame and a label and that works fine in an executable jar.

Also, I'm in a Win32 environment if that matters.

Regards,
Tom Malia
 
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,
.jar is unnecessary.

It should be :
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oleg, I'm afraid your answer is wrong - when you use the "-jar" option, you must specify the filename of the jar file, including the extension.

When you double-click a jar file to run it, it is run with javaw.exe, which is a special version of java.exe that does not open a console window. So if your program prints to the console window, nothing will be visible when you double click it.

What happens if you run it in a console window, with "java -jar Test.jar"? Nothing at all? You just get the prompt back, without any output?

Can you please post your source code and content of the manifest file?
 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
do have tool to open jar file , like ultimatezip?
 
Oleg Tikhonov
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh, yes. Sorry.
 
Marshal
Posts: 28127
94
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tom Malia wrote:HOWEVER, if I just try to execute the Jar file either from the command prompt or by double clicking on the file then, I don't get any errors but it just doesn't do anything. What am I doing wrong?



What you are doing wrong is expecting to see a console when you execute the jar that way. If you look into your Windows configuration you'll see that executable jars are configured to be run via the javaw.exe program, which doesn't use a console.
 
Tom Malia
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I kind of figured it had something to do with the while "System.out" not being the "console" when run by just executing the jar file.

as for code, it's the classic:


and the manifest file was just:

Main-Class: Test


Where is the windows configuration that tells it to use javaw? Can I change it to use java.exe instead?

What if I wanted to write a java program that I could use to process things the way you do in Unix where you pipe stuff through other program?

For example, let's say I wanted to write a java app that could be used as a filter to do something like:

c:> type MyFile.txt | MyJavaProgram.jar > NewFile.txt

can you do this?

 
Jesper de Jong
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When you run your JAR-packaged program from the command line, by executing a command such as "java -jar Test.jar", then it will not use javaw.exe - it will only use javaw.exe when you double-click the JAR file in Windows.

So if you enter "java -jar Test.jar" in a command prompt, it should print "Hello World!" in the command prompt window. If it doesn't, then something strange is happening.

I don't know if it's easy to change it so that when you double-click a JAR, java.exe instead of javaw.exe will be used. You could probably change this by changing the file extension association in Windows. You shouldn't normally do that, and it probably won't really solve your problem. When you'd double-click your JAR file with the program shown, it would open a console window, print "Hello World!" and immediately close the console window again, so you'd only see a console window flash and disappear immediately again.

Redirecting output using > or | should work in Windows from the command prompt in the same way as in a terminal window in Unix.
 
Marshal
Posts: 79019
375
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have you got a new line after the end of the text in the manifest file?
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic