简体   繁体   English

Java应用程序的类路径指定执行均返回:“错误:找不到或加载主类”

[英]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. 这不是类路径错误的情况,也不是键入java someprogram.class而不是Java someprogram的情况,这不是Linux语法错误的情况,堆栈溢出问题没有其他问题解决了我的问题。

I've been trying all sorts of stuff since yesterday now. 从昨天开始,我一直在尝试各种方法。 I'm trying to run java applications from the terminal in Linux Mint. 我正在尝试从Linux Mint的终端运行Java应用程序。 I think it may be related to two different java installations. 我认为这可能与两个不同的Java安装有关。

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. 我开始使用JDK 7和旧的Eclipse构建进行教程,但必须同时将Javafx升级到JDK 8和Eclipse 4.4.4,然后从使用Java文件夹转到Java2文件夹。 Everything in the Java folder will run from the terminal, but nothing in the Java2 folder will. Java文件夹中的所有内容都将从终端运行,但是Java2文件夹中的任何内容都不会。

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. 我尝试使用-cp modfier,现在将classpath设置为10亿个不同的目录(使用export $ CLASSPATH),这使我无所适从。 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! 它编译没有问题(那么如何将classpath变成问题?),javapackager会找到所有正确的文件! Java version, linux version, etc: Java版本,Linux版本等:

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. 在大多数情况下,您要么没有为主类提供程序包名称,要么在以下情况下没有通过-cp提供正确的类路径,这两种情况都是不正确的。

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): 应该是(pwd和java命令返回的注意路径):

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. Java类必须存储在与类本身相同的文件中。 Also it is NOT allowed to have whitespace characters in classnames. 另外,不允许在类名中包含空格字符。 See eg http://docstore.mik.ua/orelly/java-ent/jnut/ch07_01.htm 参见例如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. 当创建jar时,您使用的文件名不正确。 It should be helloWorld.java 应该是helloWorld.java

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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