aspose file tools*
The moose likes Beginning Java and the fly likes jar Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "jar" Watch "jar" New topic
Author

jar

Justin Fox
Ranch Hand

Joined: Jan 24, 2006
Posts: 802
ok, i know how to create a jar file.

but when you create it, when you double click on it, does it automatically

execute the main class?

or do you have to modify the manifest file?

-Justin-


You down with OOP? Yeah you know me!
Justin Fox
Ranch Hand

Joined: Jan 24, 2006
Posts: 802
ok, i figured how to make it "executable" through the command line,

but what about when you double click on the file itself, nothing happens.

what do i need to do to get it to open when i double click on it

-Justin
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24184
    
  34

The file "META-INF/MANIFEST.MF " in the jar file must contain a line like

Main-Class: com.justin.ClassName

and then your desktop has to be set up to associate JAR files with java.exe.


[Jess in Action][AskingGoodQuestions]
Justin Fox
Ranch Hand

Joined: Jan 24, 2006
Posts: 802
ok i changed the desktop to where it opens jar files with java.exe, but

and blank black box pops up and then goes away really fast...

why is that?

-Justin-
Justin Fox
Ranch Hand

Joined: Jan 24, 2006
Posts: 802
i have the class called AVerage.class

and i did

jar cfm Average.jar Manifest.txt AVerage.class.

and in the Manifest.txt

it says.

Main-Class: AVerage

-Justin-
Justin Fox
Ranch Hand

Joined: Jan 24, 2006
Posts: 802
help me plz
Vishnu Prakash
Ranch Hand

Joined: Nov 15, 2004
Posts: 1026
Search this forum with keyword jar. I remember one of the bartenders here has written a series of articles regarding jar and related stuff.


Servlet Spec 2.4/ Jsp Spec 2.0/ JSTL Spec 1.1 - JSTL Tag Documentation
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41839
    
  63
A quick search through the FAQ finds this page, which has some hints and links to further information.

If it terminates quickly, then that probably means there was an uncaught exception. Try running it from the command line like "java -jar xyz.jar". Then you'll have a chance to see the stack trace of the exception.
[ May 03, 2006: Message edited by: Ulf Dittmer ]

Ping & DNS - my free Android networking tools app
Peter Brockway
Greenhorn

Joined: Oct 12, 2005
Posts: 19
ok i changed the desktop to where it opens jar files with java.exe, but and blank black box pops up and then goes away really fast...

You should change the association so that Windows
uses javaw.exe to open the .jar file. (The difference
is that you don't get the console window.)

This is usually the default, but it might have got
changed somehow. You can right click on a .jar file
and choose "Open with..." to change the association.

Ernest's point about the manifest is important - otherwise
"nothing" will happen when you double click the .jar
file. Sometimes you actually want to see the console
output - for error messages when the program is starting,
or for debugging output that you print to System.out. One
way to achieve this is to run the program from the command
line with:
Justin Fox
Ranch Hand

Joined: Jan 24, 2006
Posts: 802
yeah, it works fine in the command prompt

and tried to get javaw.exe to open it and the hour glass came up and that

was it....

i did everything right...

guess my comp sucks or something

-Justin-
Peter Brockway
Greenhorn

Joined: Oct 12, 2005
Posts: 19
It might be a good idea to put a System.out.println("!!!") at the start of your main() method. You should be able to see this from the command prompt when you use the java -jar command. This will test that there is nothing wrong with the manifest.

When you say it works fine from the command prompt, what actually happens? I ask this because to be visible your program has to produce some non-console output (eg make a JFrame visible or whatever). What you are expecting to see is that running javaw.exe (via a double click) has exactly the same result as running java -jar (except for console output).

If the code is not too long perhaps you could post it.
Justin Fox
Ranch Hand

Joined: Jan 24, 2006
Posts: 802
this is what my mainfest file contains:



and here is the java file AVerage.java:



the program is very simple..

and when i do the: java -jar Average.jar, that works just fine,

the out put looks like this:



because i created my file like this: jar cmf Averagefest.txt Average.jar AVerage.class

all i want is for the command line to pop up when i double click on the
file..

or does it only work if you jar a applet?

thanks in advance

-Justin-
[ May 04, 2006: Message edited by: Justin Fox ]
Rusty Shackleford
Ranch Hand

Joined: Jan 03, 2006
Posts: 490
Peter Brockway gave you the answer as to why, reread it. You have no non-console output.
[ May 04, 2006: Message edited by: Rusty Shackleford ]

"Computer science is no more about computers than astronomy is about telescopes" - Edsger Dijkstra
Justin Fox
Ranch Hand

Joined: Jan 24, 2006
Posts: 802
and if the double click only works with a JFrame, can i display that

application in my own frame?

but still the the functionality that i want?

for example:

when i double click, my own JFrame pops up, but still has output inside it

like so:



and would the JFrame act just as a command prompt?

-Justin-
Justin Fox
Ranch Hand

Joined: Jan 24, 2006
Posts: 802
you told me that he told me the answer, but it must have been a subliminal

message, because i didn't see an answer just assumtions(not to be an Ahole)

I have posted the code, and have asked my question about the JFrame thing.

i mean i understand that i have to have an applet or jframe for the double

click to work, but i still have my other question floating around.

waiting...

-Justin-
[ May 04, 2006: Message edited by: Justin Fox ]
Peter Brockway
Greenhorn

Joined: Oct 12, 2005
Posts: 19
you told me that he told me the answer, but it must have been a subliminal message, because i didn't see an answer just assumtions

Sorry, I may not have been very clear. But I think you've figured out that using javaw to run a .jar file is not good if your program relies on console input and output.

As an alternative you could write a short script (.bat or .cmd) file to launch your .jar file. For instance
;File: c:\AVerage.bat
"Program Files\Java\jdk1.5.0_06\bin\java" -jar AVerage.jar
is quite effective in running your program in a console window. (Note the lack of "w" in the command name). There is still a problem because the window closes before you can read the answer! One way around this is to add a prompt, "Press <Enter> to exit..." and read a line of input from the console before exiting.

(There may be other bugs in your program, but they don't have anything to do with running it as a .jar)

To address your other question: you could write your own Swing component that interacts textually with the user, but this is not easy. Probably it is better if you are writing a graphical application to do things graphically (use controls and dialog boxes to gather and display information). And if you are using Scanner, launch the .jar with a script as described above.

Do (continue to) ask if I'm being unclear. There's a bit of a time lag - I guess we're in different time zones.
[ May 05, 2006: Message edited by: Peter Brockway ]
Justin Fox
Ranch Hand

Joined: Jan 24, 2006
Posts: 802
I made an applet and jar'd it and the double click works just fine.

thanks for all the help, i guess for my average class i'll have to make it

a GUI to be able to double click it and use it.

but all i would have to do is have labels,

one for the line "Enter Number to be Averaged: "

and then one for "Enter the "+count+" Number: " // then have a

JTextfield 't' for input.

then i could do :

int answer = Integer.parseInt(t.getText());
int sum = 0;
sum += answer;

and have some buttons, "add" and "average".

anyways, thanks again.

-Justin-
[ May 05, 2006: Message edited by: Justin Fox ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: jar