• 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

Making Jars with Ant

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
G'Day all,

Please forgive the seemingly simple question, but i have run into a brick wall over this. I have RTFM, RTFT and STFW & I am stuck.

I am doing a simple Hellowrld app, within a package. Now I can get it to comile without issue with Ant, I can even create a .jar file, but I cannot get it to run.

When I "java -jar HelloWorld.jar", I get the following error.

Exception in thread "main" java.util.zip.ZipException: The system cannot find the 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)

The build.xml file looks like this

<?xml version="1.0" encoding="UTF-8"?>
<project name="Helloworld" default="makejar" basedir=".">
<target name="compile">
<javac srcdir="."/>
</target>

<target name="makejar" depends="compile"
description="Create a jar for the Helloworld project">
<jar jarfile="HelloWorld.jar" includes="**/*.class" basedir="."/>
</target>
</project>


Have I missed anything? If so, what? And are there any useful for newbie documentation floating around anywhere?

Thanking you in advance for your help,

Craig.
 
Ranch Hand
Posts: 451
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Craig Gaffney:
G'Day all,

Please forgive the seemingly simple question, but i have run into a brick wall over this. I have RTFM, RTFT and STFW & I am stuck.

I am doing a simple Hellowrld app, within a package. Now I can get it to comile without issue with Ant, I can even create a .jar file, but I cannot get it to run.



Which directory are you running the java -jar command from? From the directory where HelloWorld.jar is located? Try cd to that directory. You may want to try a relative path name such as:

java -jar ./HelloWorld.jar (Unix)
java -jar .\HelloWorld.jar (Windows)


You may also be looking for
this. Page down and look at the section about creating executable jars. Perhaps.

I'd like a look at the contents of your manifest.mf file, if possible.
 
Craig Gaffney
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
G'Day Don,

I am executing the java -jar command in the same (windows) directory as the .jar file. Even adding in the .\ infront of the jar file name made no difference.

The manifest.mf file, as taken from the .jar file is as follows

Manifest-Version: 1.0
Ant-Version: Apache Ant 1.6.1
Created-By: 1.4.2_04-b05 (Sun Microsystems Inc.)

From the link you sent me, this looks like there is something missing - a reference to the main class? (in this case HelloWorld)
If that is the case, how do I get the requried information into the manifest file?

Thanks for the help so far,

Craig
 
Don Stadler
Ranch Hand
Posts: 451
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Craig, I assume that one of the classes in your jar file has a main() method in it? The link I gave you in the post above shows how to create an executable jar file.

Assuming that your jar file is in the correct format and order, you can create a manifest file under the WEB-INF directory (call it manifest.mf to keep this simple). In manifest.mf put the following:



This assumes that the name of the source file containing the main() method is Hello.java and the name of the resultant classfile is Hello.class. Then
create the executable jar file as usual. This should work, but if it doesn't you will have to look into the build file or possibly do this manually with the following command:

jar cmf WEB-INF/manifest.mf <jar-file> <input-files>

the <jar-file> is a marker for whatevery you are calling the jar file and the <input-files> include the class files going into the jar.

Hope this helps.

 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Since this isn't really about Ant, I'm moving this off to the Java in General(intermediate) forum.
 
Craig Gaffney
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
G'Day Bear,

I tend to disagree with your decision, as in the original post I made reference to using ant, and included the build.xml file. I am still trying to determine how to do this with Ant, so if anyone knows how to get thsi to work, I'll appreciate it alot.


Thanks,

Craig.
 
Craig Gaffney
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
G'Day Don,
Problem solved. There were two(2) issues.
1) typo HellowWorld != HelloWorld
2) Main-Class in the manifest needed to have the package hierarchy included
ie Main-Class: path.to.class.HelloWorld

Thanks for helping me to understand the basics!

Also, I found out how to do up the jar task in Ant to do it!

Once more, Thanks Don.
 
It sure was nice of your sister to lend us her car. Let's show our appreciation by sharing this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic