aspose file tools*
The moose likes Java in General and the fly likes running a class in a jar ...... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of JavaScript Promises Essentials this week in the JavaScript forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "running a class in a jar ......" Watch "running a class in a jar ......" New topic
Author

running a class in a jar ......

Steven Broadbent
Ranch Hand

Joined: Dec 10, 2002
Posts: 400
I am using ant to jar up an application and it is going ok.
However I have two minor problems which I think are class path related.
First how do I run a class from the jar I create, I want to run java from inside the directory that contains the jar.
Secondly how can I have a build.xml for ant in each projects src dir and run ant from inside the src dir.


"....bigmouth strikes again, and I've got no right to take my place with the human race...."<p>SCJP 1.4
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
First how do I run a class from the jar I create
You'll need the JAR file included in your CLASSPATH setting, or you can create an executable JAR file.


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Steven Broadbent
Ranch Hand

Joined: Dec 10, 2002
Posts: 400
Still have problem running the single class jar:-
C:\j2sdk1.4.2_01\bin>java -jar C:\javaprojects\ant_test\dist\project.jar wibble
Failed to load Main-Class manifest attribute from
C:\javaprojects\ant_test\dist\project.jar
the jar was created with ant, has I class wibble with a main method
Steven Broadbent
Ranch Hand

Joined: Dec 10, 2002
Posts: 400
followed the example on creating a jar given by Dirk, but double clicking the jar gives error message "exception in main thread" (I think, the console window flashes by )

Any answers anyone? I can create jars it seems, but can't run them via double click of the command prompt.
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
Can you run them from a command prompt? If so, then it's likely a case of the JAR file type not being properly registered with your OS in order to be able to run them by double-clicking on them.
If you can run them from a command prompt, and you'd like some help with adjusting your OS settings to work with double-clicks, just post a note.
Note that the syntax to run execute a JAR from a command prompt is java -jar MyJar.jar
Steven Broadbent
Ranch Hand

Joined: Dec 10, 2002
Posts: 400
my jar is under C:
C:\>C:\j2sdk1.4.2_01\bin\java -jar MyFoo.jar
Exception in thread "main" java.util.zip.ZipException: The system cannot f
e file specified
at java.util.zip.ZipFile.open(Native Method)
at java.util.zip.ZipFile.<init>(ZipFile.java:112)
at java.util.jar.JarFile.<init>(JarFile.java:127)
at java.util.jar.JarFile.<init>(JarFile.java:65)
seems I have had every error message imaginable. I have look into the
jar and it seems to be ok - a single class with no package dec.
Brian Pipa
Ranch Hand

Joined: Sep 29, 2003
Posts: 299
If it helps, here is my jar target that I use to create an executable jar. Maybe you can modify it to jar up your code.

Brian


My Java Freeware:<br />MACCC - <a href="http://maccc.pipasoft.com" target="_blank" rel="nofollow">http://maccc.pipasoft.com</a><br />Nator - <a href="http://nator.pipasoft.com" target="_blank" rel="nofollow">http://nator.pipasoft.com</a>
Steven Broadbent
Ranch Hand

Joined: Dec 10, 2002
Posts: 400
Still can't get this working.
Brian Pipa
Ranch Hand

Joined: Sep 29, 2003
Posts: 299
>Still can't get this working.
>
That doesn't tell us much. What did you try? What errors do you get (if any)? Give us some code, some command lines, some build scripts, etc., etc. We're not mind readers
brian
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: running a class in a jar ......