views:

571

answers:

2

I've compiled a java project into a Jar file, and am having issues running it.

When I run:

java -jar myJar.jar

I get the following error

Could not find the main class: myClass

The class file is not in the root directory of the jar so I've tried changing the path of the main class to match the path to the class file and I get the same issue.

Should I be flattening the file structure? if so how do I do this. I'm using Ant to build the Jar file if thats of any use.

UPDATE

Here is the contents of the jar and the relevant Ant sections, I've changed the name of the firm I work for to "org":

META-INF/
META-INF/MANIFEST.MF
dataAccessLayer/
dataAccessLayer/databaseTest.class
org/
org/eventService/
org/eventService/DatabaseObject.class
org/eventService/DatabaseObjectFactory.class
org/eventService/DbEventClientImpl$HearBeatMonitor.class
org/eventService/DbEventClientImpl.class
org/eventService/EmptyQueryListException.class
org/eventService/EventHandlerWorkItem.class
org/eventService/EventProcessor.class
org/eventService/EventTypeEnum.class
org/eventService/EventWorkQueue$MonitorThread.class
org/eventService/EventWorkQueue$PoolWorker.class
org/eventService/EventWorkQueue.class
org/eventService/FailedToLoadDriverException.class
org/eventService/IConnectionFailureListener.class
org/eventService/InvalidEventTypeException.class
org/eventService/JdbcInterfaceConnection.class
org/eventService/NullArgumentException.class
org/eventService/OracleDatabaseObject.class
org/eventService/ProactiveClientEventLogger.class
org/eventService/ProactiveClientEventLoggerException.class
org/eventService/PropertyMap.class
org/eventService/SQLServerDatabaseObject.class
org/eventService/TestHarness.class
org/eventService/Utilities.class

And the ant target:

<target name="compile" depends="init" description="compile the source ">
    <javac srcdir="src" destdir="bin" classpathref="project.class.path"/>
</target>
<target name="buildjar" description="build jar file" depends="compile">
    <mkdir dir="dist"/>
    <jar destfile="dist/myJar.jar" basedir="bin" includes="**/*.class" >
        <manifest>
            <attribute name="Main-Class" value="org.eventService.ProactiveClientEventLogger"/>
        </manifest>
     </jar>
</target>
+6  A: 

In your manifest file, make sure you have the attribute Main-Class set to the name of the class containing your main() method. For example, if the package-qualified name of the class is my.cool.Class, then, in your JAR manifest, Main-Class should be set to my.cool.Class.

Also made sure that you have a package declaration in your .java files (for example, in Class.java, make sure you have the proper package my.cool; declaration). Also, make sure your directory hierarchy is set up correctly (my.cool.Class should be in $SRC/my/cool/Class.java).

mipadi
I've done all that... and it's still not working... Although I'd used slashed and not dots to deliminate them. it doesn't work with dots either...
Omar Kooheji
The manifest files are very picky about capitalization, white space and so on. Make sure your lines are properly terminated too if the Main-Class line is the last one.
John Burton
+4  A: 

You should specify your main class during jar creation with full path, something like:

<target name="jar" depends="compile">
     <delete file="myJar.jar"/>
     <delete file="MANIFEST.MF"/>
     <manifest file="MANIFEST.MF">
        <attribute name="Main-Class" value="my.package.myClass"/>
    </manifest>

    <jar destfile="myJar.jar"
           basedir="."
           includes="**/*.class"
           manifest="MANIFEST.MF" />
</target>
asalamon74