• 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

jar files and the console

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the ranch.

You need to set the Main-Class in the MANIFEST file. Check this.
 
Matthew Mellott
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
May because you don't a carriage-return after "Main-Class: Test"
 
Matthew Mellott
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A jar is usually opened with a program called javaw which does not open a console or terminal.
 
Matthew Mellott
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nice solution, but if you are distributing .jars you will want them to be usable without a command line.
 
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yup, I would not not plan on distributing a command line application (those are just sketchy nowadays).
 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is quite simple.

1. Open your Windows command windows, CD to your directory where you have the jar file. Like:

if it's a disk other than the current, you must add "/d"

Or, simply enough,  shift+right-click in the folder, to "open command here".

2. If you have installed Java JDK, you can use Java to run this jar. Remember to enter full jar name. "My*" is not ok.


3. Now you see your standard out in command line window.

 
If I had asked people what they wanted, they would have said faster horses - Ford. Tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic