简体   繁体   中英

No matter classpath designation execution of java application returns: “Error: Could not find or load main class”

Former question was closed stating it was a duplicate question, but it is not. I have searched for hours and hours now. This is not a case of wrong class path, this is not a case of typing java someprogram.class instead of java someprogram, this is not a case of wrong Linux syntax, no other question on stack overflow solves my problem.

I've been trying all sorts of stuff since yesterday now. I'm trying to run java applications from the terminal in Linux Mint. I think it may be related to two different java installations.

I began doing tutorials with JDK 7 and an old Eclipse build, but had to upgrade both to JDK 8 and Eclipse 4.4.4 for javafx, and in the process I went from using a folder called Java to a folder called Java2. Everything in the Java folder will run from the terminal, but nothing in the Java2 folder will.

The error boils down to this:

This works:

dalsgaard@dalsgaard $ pwd
/home/dalsgaard/Skrivebord/Java/tutorials/bin
dalsgaard@dalsgaard $ java Welcome
Hej med dig!

This does not:

dalsgaard@dalsgaard $ pwd
/home/dalsgaard/Skrivebord/Java2/tutorials/bin/application
dalsgaard@dalsgaard $ java helloWorld 
Error: Could not find or load main class helloWorld

This does not work either:

dalsgaard@dalsgaard $ java -cp /home/dalsgaard/Skrivebord/Java2/tutorials/bin/application/ helloWorld`
Error: Could not find or load main class helloWorld

Nor does this:

dalsgaard@dalsgaard $ java -cp . helloWorld`
Error: Could not find or load main class helloWorld

I have tried using the -cp modfier, and setting the classpath (Using export $CLASSPATH) to a billion different directories now, and it's driving me up the wall. As you can see, I have a main class:

package application;

public class helloWorld {

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

    }

}

I have tried the following:

dalsgaard@dalsgaard $ javapackager -createjar -appclass application.TicTacToeRandom -outdir . -outfile outjar -srcdir . -v
dalsgaard@dalsgaard $ java -jar outjar.jar
Error: Could not find or load main class Application.TicTacToeRandom
dalsgaard@dalsgaard $ 

It compiles no problem (So how can classpath be the issue?), and javapackager finds all the right files! Java version, linux version, etc:

dalsgaard@dalsgaard $ java -version
java version "1.8.0_25"
Java(TM) SE Runtime Environment (build 1.8.0_25-b17)
Java HotSpot(TM) 64-Bit Server VM (build 25.25-b02, mixed mode)
dalsgaard@dalsgaard $ uname -a
Linux dalsgaard 3.13.0-24-generic #47-Ubuntu SMP Fri May 2 23:30:00 UTC 2014 x86_64 x86_64 x86_64 GNU/Linux

Most of the cases you are either not supplying the main class with package name or you are not supplying proper class path via -cp for instance below, both is incorrect.

dalsgaard@dalsgaard $ pwd
/home/dalsgaard/Skrivebord/Java2/tutorials/bin/application
dalsgaard@dalsgaard $ java helloWorld 
Error: Could not find or load main class helloWorld

It should be (notice path returned by pwd and java command):

dalsgaard@dalsgaard $ pwd
/home/dalsgaard/Skrivebord/Java2/tutorials/bin/
dalsgaard@dalsgaard $ java application.helloWorld 

Java classes has to be stored in files with the same as the class has itself. Also it is NOT allowed to have whitespace characters in classnames. See eg http://docstore.mik.ua/orelly/java-ent/jnut/ch07_01.htm

For example in the following snippet you've put whitespace character in your command line:

dalsgaard@dalsgaard $ java -cp /home/dalsgaard/Skrivebord/Java2/tutorials/bin/application/ helloWorld`
Error: Could not find or load main class helloWorld

And when creating jar you're using incorrect file name. It should be helloWorld.java

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