This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark ""Hello world" not printing from executable jar" Watch ""Hello world" not printing from executable jar" New topic
Author

"Hello world" not printing from executable jar

Tom Malia
Greenhorn

Joined: Aug 05, 2009
Posts: 22
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
Oleg Tikhonov
Ranch Hand

Joined: Aug 02, 2008
Posts: 55
Hello,
.jar is unnecessary.

It should be :
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14278
    
  21

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?


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
krishna bala
Ranch Hand

Joined: Jul 20, 2009
Posts: 61
do have tool to open jar file , like ultimatezip?
Oleg Tikhonov
Ranch Hand

Joined: Aug 02, 2008
Posts: 55
Oh, yes. Sorry.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18708
    
    8

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

Joined: Aug 05, 2009
Posts: 22
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
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14278
    
  21

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.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39478
    
  28
Have you got a new line after the end of the text in the manifest file?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: "Hello world" not printing from executable jar