Cannot Find Main Class in File Compiled with Ant

Cannot find Main Class in File Compiled With Ant

Looks like your runtime classpath is missing the jar containing the class org.supercsv.io.ICsvBeanReader.

The gotcha is that you cannot set the classpath from the command-line when calling an executable jar. You have to set it within the manifest as follows:

<target name="dist" depends="compile" description="Generates distributable">
<!-- creates the distribution directory -->
<mkdir dir="${dist}/lib" />

<!-- Remove manifest. This jar will end up on the classpath of CC.jar -->
<jar jarfile="${dist}/lib/CC-${DSTAMP}.jar" basedir="${build}"/>

<!-- Fancy task that takes the pain out creating properly formatted manifest value -->
<manifestclasspath property="mf.classpath" jarfile="${dist}/lib/CC.jar">
<classpath>
<fileset dir="${dist}/lib" includes="*.jar"/>
</classpath><!--end tag-->
</manifestclasspath>

<!-- This is the executable jar -->
<jar jarfile="${dist}/lib/CC.jar" basedir="${build}">
<manifest>
<attribute name="Main-Class" value="jab.jm.main.Test"/>
<attribute name="Class-Path" value="${mf.classpath}"/>
</manifest>
</jar>

</target>

This approach will allow you to run the jar as follows:

java -jar CC.jar

Without the extra manifest entry you have to run the jar as follows:

java -cp CC.jar:CC-DSTAMPVALUE.jar jab.jm.main.Test

Note

Only the CC.jar is executable and needs the special manifest. Using this pattern means future additional jars, placed into the lib directory, will be automatically included in the run-time classpath. (Useful for open source dependencies like log4j)

Obviously, when running the CC.jar you'll get a similar error if the jar files are not present :-)

Could not find the main class error with Ant

Your run target looks a bit wonky, the <java classname="${main-class}" fork="true" /> is self closing, so the classpath element is never used

<target name="run" depends="jar">
<java classname="${main-class}" fork="true" >
<classpath>
<path refid="classpath" />
<path location="${jar.dir}/${ant.project.name}.jar" />
</classpath>
</java>
</target>

May work for you.

Could not find or load main class Java

Your code depends on libraries. You have successfully added the libraries in the classpath to compile your code, and have thus successfully created an executable jar files containing your classes. But these classes depend on libraries to run. And when running the jar file, you don't secify anywhere that Java should look for classes in libraries in addition to your jar file.

See Generate manifest class-path from <classpath> in Ant for how to add a Class-Path entry to the manifest of your executbale jar file. Beware: the paths of the libraries must be relatie to the path of the jar.

Ant is a bit outdated. If I were you, I'd try using gradle, which has a neat application plugin doing all that for you, and much more.

Could not find or load main class NotifyAdministrator in java ant

You have to provide classpath for your class in java ant task (http://ant.apache.org/manual/Tasks/java.html).

For instance:

<java fork="true" failonerror="yes" classpath="." classname="NotifyAdministrator">
<arg line="admin@test.com"/>
</java>

assuming that NotifyAdministrator.class is in the same directory as build.xml and that you invoke ant in that directory.

Java - Ant build (Eclipse) - Could not find the main class: nat.rutherford.DesktopStarter

The problem is in your task dist when you create your jar. If your compilation is right and there is no problem when you package your jar. Things that are wrong:

  • <mkdir dir="${dist}/lib"/> -> this don't have mean, you don't use it never

  • second you are not including your libraries in your jar, then when you try to execute your jar it doesn't work that why you are seeing the error message Could not find the main class: nat.rutherford.DesktopStarter. Program will now exit You could see that your libraries aren't within your jar using Winzip or similar. I suppose that you are seeing your problem when you try to execute the jar directly using windows or similar. A good way to see what is happening, seeing the problem printed in the console is executing your jar in the next way: java -jar MyProject-20120102.jar

  • See: How to include your libraries in you jar?

  • And if you want to know more about jar packaging using ant try this.

  • Another thing that you need to modify the Class-path attribute in your manifest to include the libraries within your ${libs} folder.

Could not find or load main class when Running java program using Ant?

You have not included the output dir (".") of your javac task on the classpath of your java task, so the ForDemo class that you compile there is not on the classpath when java executes.

You could include that dir in your java task like this:

    <classpath>
<pathelement path="${classpath.base}"/>
<pathelement location="."/>
</classpath>


Related Topics



Leave a reply



Submit