简体   繁体   中英

Could not find or load main class- I cannot run my .jar file created from Ant

I have been struggling with this for two days now. I created a very simple HelloWorld class to test if I can get this working but I was not able to. I get Error- Could not find or load main class... It works from Eclipse or run task from the script. But double-clicking .jar or running it from CMD gives me the error. What are some possible reasons for this error? Class-path? environmental variables? directory structure? Please help!

package com.hellojava;

public class HelloWorld {

 public static void main(String[] args)
 {
    System.out.println("Hello World!");
 }

}

My build.xml

<project name="TestProject" basedir="." default="main">

<property name="src.dir"     value="src"/>
<property name="build.dir"   value="build"/>
<property name="classes.dir" value="${build.dir}/classes"/>
<property name="jar.dir"     value="${build.dir}/jar"/>
<property name="main-class"  value="com.hellojava.HelloWorld"/>

<target name="clean">
    <delete dir="${build.dir}"/>
</target>

<target name="compile">
    <mkdir dir="${classes.dir}"/>
    <javac srcdir="${src.dir}" destdir="${classes.dir}"/>
</target>

<target name="jar" depends="compile">
    <mkdir dir="${jar.dir}"/>
    <jar destfile="${jar.dir}/${ant.project.name}.jar" basedir="${classes.dir}">
        <manifest>
            <attribute name="Main-Class" value="${main-class}"/>
        </manifest>
    </jar>
</target>

<target name="run" depends="jar">
    <java jar="${jar.dir}/${ant.project.name}.jar" fork="true"/>
</target>

<target name="clean-build" depends="clean,jar"/>

<target name="main" depends="clean,run"/>

run:
 [java] Hello World!
main:
BUILD SUCCESSFUL
Total time: 5 seconds

Manifest-Version: 1.0
Ant-Version: Apache Ant 1.9.2
Created-By: 1.7.0_11-b21 (Oracle Corporation)
Main-Class: com.hellojava.HelloWorld

**Edited: java -jar TestProject.jar works but javaw -jar TestProject.jar does not. However, I solved the problem- see answer I posted.

The usual syntax to run a jar file is java -jar TestPractice.jar . If this fails, some analysis is required.

  1. Open your jar file with WinZip, and extract to a temporary directory.
  2. Check that HelloWorld.class file is present, and in the correct directory.
  3. Check that there is a META-INF/MANIFEST.MF file, and it contains a line reading Main-Class: com.hellojava.HelloWorld I don't see anything in your Ant script that would generate this file.

If all that is OK, your program will run.

For more information, take a look at how to create a bundled runnable jar using Ant

Problems Solved!

I had to change the registry to a correct Java version that I am using. It was set to the previous version of Java that was in my comp.

I can run .jar files through cmd, but I cannot double click them

Also, I noticed that javaw -jar file.jar does not work for me while java -jar file.jar works. I changed the program that opens .jar files by "open with" and select java instead of javaw. This now solves the problem- I do have one more problem though. I wonder why javaw does not work but for now I'm happy this is working :)

I had the same problem. Application worked fine on my PC but on some PCs I get the same error. Try to upadte Java runtime on machines where your app donesn't work. For example I worked with JRE7 and on PC where app was not working there was Java 1.6 installed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM