简体   繁体   中英

Java compilation error: could not find or load main class Main

I've tried many configurations and parameter settings, but keep getting Error: Could not find or load main class Main error.

Most of the code I've copied from other SO questions and the documentation of the ant project, and I don't really see where the issue lies. Perhaps someone can see what I'm overlooking.

Directory structure

├───build
│   ├───com
│   │   └───cfsware
│   │       └───osco
│   │           └───test
│   └───META-INF
├───dist
│   └───lib
└───src
    └───com
        └───cfsware
            └───osco
                └───test

build.properties

main.dir=. 
src.dir=${main.dir}/src 
build.dir=build 
classes.dir=${build.dir}/classes 
jar=${build.dir}/test.jar 
javadoc.dir=${build.dir}/javadoc 
build.sysclasspath=ignore 
# E.g.: cp=lib/x.jar:lib/y.jar 
cp= 
extra.run.cp= 
main.class=com.cfsware.osco.test.Main 
run.cp=${cp}:${classes.dir}:${extra.run.cp} 
debug=true 
deprecation=false 
nbjdk.home=${basedir}/../../.. 

build.xml

<?xml version="1.0" encoding="UTF-8"?> 
<project name="ant" default="dist" basedir="."> 
<description>ant build file</description> 
<property name="src" location="src"/> 
<property name="build" location="build"/> 
<property name="dist" location="dist"/> 
<target name="init"><tstamp/><mkdir dir="${build}"/></target> 
<target name="compile" depends="init" description="compile the source"><javac srcdir="${src}" destdir="${build}"/></target> 
<target name="dist" depends="compile"> 
    <mkdir dir="${dist}/lib" /> 
    <manifest file="${build}/META-INF/MANIFEST.MF"> 
        <attribute name="Class-Path" value="test.jar"/> 
        <attribute name="Main-Class" value="Main"/>   
    </manifest> 
    <jar manifest="${build}/META-INF/MANIFEST.MF" jarfile="${dist}/lib/test.jar" basedir="${build}"/> 
</target> 
<target name="clean" description="clean up"><delete dir="${build}"/><delete dir="${dist}"/></target> 
</project> 

Main.java

package com.cfsware.osco.test; 
public class Main{ 
    public static void main(String[] args) throws Exception{ 

    } 


} 

I suspect this is the problem:

<attribute name="Main-Class" value="Main"/>

It should be:

<attribute name="Main-Class" value="com.cfsware.osco.test.Main"/>

... in other words, the fully-qualified name of the class. Your Java file should be in a directory matching the package declaration, too.

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