简体   繁体   中英

Can't run compiled java file with main method

I have an application, which I try to build using Maven. I can build a jar, but running it does nothing: it can't find a Main class. I set however the MainClass in the POM. When I try to run the MainPane.class in the target folder from the command line returns: "Error: could not find or load Main class MainPane". (After navigating inside the target folder and run 'java MainPane')

I can run this class from Eclipse and it has a main method:

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

I should be able to run the class file in the target folder right? What can possibly go wrong?

You need to tell java where the class is by defining the -classpath parameter

java -classpath classes MainPane

Or from the project directory

java -classpath target/classes MainPane

Also make sure you are using the full package name if you have one

java -classpath target/classes my.package.name.MainPane

@AndyGeeDe

TomEE treibt Tomitribe! | http://tomee.apache.org

java -jar myjar.jar

Will only work if the jar's manifest contains the "Main-Class: classname" entry. Check for that. If it is not present then you can still run you application via this command:

java -cp ./my.jar package/class_name

Try googling on "maven executable jar". Once you get the build correct, you can run it using

java -jar myjar.jar

Java is not terribly good at this. The executable jar needs all the dependent jars unpacked, and a manifest file included that names the main class. To my mind is less than elegant but it does work.

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