This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes jar files and the console Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "jar files and the console" Watch "jar files and the console" New topic
Author

jar files and the console

Matthew Mellott
Greenhorn

Joined: Nov 29, 2010
Posts: 14
My problem: the console is not opening when I double click my jar file.

When I run the jar file from command prompt (using >java -jar Main.jar), everything works the way I expect it to.

Here is the batch file I use to build my program (I was originally using eclipse but I encountered this same problem and thought eclipse was to blame):

Here is the Main.java file(just an example):

So, in other words, can you add something that forces the console to open?
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

Welcome to the ranch.

You need to set the Main-Class in the MANIFEST file. Check this.


[My Blog]
All roads lead to JavaRanch
Matthew Mellott
Greenhorn

Joined: Nov 29, 2010
Posts: 14
Thanks for welcoming me to the ranch.

So now I'm having a diffrent problem. When I double click the jar file, its saying that it can't load my Main-Class manifest attribute. Here is my code:

MANIFEST.MF:

build.bat:

test.java:
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

May because you don't a carriage-return after "Main-Class: Test"
Matthew Mellott
Greenhorn

Joined: Nov 29, 2010
Posts: 14
Ok, so I did some more googling and finally found an answer:
A JAR is a great way of packaging and deploying a Java SE project. But there is a problem with console input/output--the standard I/O files (System.out, System.err, and System.in) do not work when a JAR is activated by a double-click. System.out and System.err are simple, intuitive, and frequently used for problem reporting, and their loss can be quite inconvenient.

http://today.java.net/article/2006/01/27/console-terminal-jars

So basically there is no "easy" solution to this problem. The website offers an open source tool that they say you can use to fix the problem without changing any code.

edit: so i'm no longer sure what this webiste is talking about anymore. setting .jar files to opened with java.exe when you double click them works fine for console programs and therefore their solution seems overly complicated.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39095
    
  23
A jar is usually opened with a program called javaw which does not open a console or terminal.
Matthew Mellott
Greenhorn

Joined: Nov 29, 2010
Posts: 14
Campbell Ritchie wrote:A jar is usually opened with a program called javaw which does not open a console or terminal.

Oh ok that makes sense. So I guess the fix is simpler then I thought, you just have to tell windows to open it with java.exe instead of javaw.exe.

Thanks!

Edit: here is how I finally got around this problem if anyone else is interested (this is all in windows).

Since I didn't want all jar files to be opened with java.exe (who wants a console popping up when you have a flashy gui?), I just added a right click option to jar files that says "open in console" and causes the jar file to be opened with java.exe. I did this by just quickly going in to the registry; click here for more info.

If you don't care about having the console popping up every time you double click a jar file, you can use the following in cmd.exe to make jar files open with java.exe:

ftype jarfile=java.exe -jar "%1"
(this is assuming you have your classpath set already and jarfile is already associated with .jar. google classpath, assoc, and ftype for more info.)

Hope this helps someone.

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39095
    
  23
Nice solution, but if you are distributing .jars you will want them to be usable without a command line.
Alex Hurtt
Ranch Hand

Joined: Oct 26, 2010
Posts: 98
As a "better" solution you could redirect the System.out and/or System.err to specific files or a console of your own devising via System.setOut() and System.setErr().
Or you could also do as this blog author suggests and redirect System.out and System.err to the java logging subsystem (Scroll down about 3/5 of the page for the example code to do it).
Matthew Mellott
Greenhorn

Joined: Nov 29, 2010
Posts: 14
Yup, I would not not plan on distributing a command line application (those are just sketchy nowadays).
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: jar files and the console