错误:无法找到或加载主类 xxx Linux

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17743430/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-07 00:18:40  来源:igfitidea点击:

Error: Could not find or load main class xxx Linux

javalinuxshell

提问by user1660325

I am very new to linux environment.

我对linux环境很陌生。

I am trying to run an simple hello world java class in linux environment.

我正在尝试在 linux 环境中运行一个简单的 hello world java 类。

Hello .java

你好.java

 package com.util;

    public class Hello {

        /**
         * @param args
         */
        public static void main(String[] args) {
            System.out.println("hi");

        }

    }

I have compiled java class in windows environment and uploaded the .class file to linux system into /home/scripts path.

我已经在windows环境中编译了java类并将.class文件上传到linux系统到/home/scripts路径。

my command is as follows,

我的命令如下,

java -cp /home/scripts com.util.Hello

when i am executing this command from this same /home/scripts where Hello.class is there i am getting,

当我从相同的 /home/scripts 中执行此命令时,Hello.class 在那里,我得到了,

Error: Could not find or load main class com.util.Hello and not able to proceed further.

错误:无法找到或加载主类 com.util.Hello 并且无法继续进行。

can some one help me in this issue?

有人可以帮助我解决这个问题吗?

采纳答案by Shashi

navigate to /home/scripts using terminal

使用终端导航到 /home/scripts

javac com/util/Hello.java 

then

然后

cd /home/scripts
java -cp . com.util.Hello

Or,

或者,

java -cp "/home/scripts" com.util.Hello   

回答by L. Quastana

At first you must generate your .class file :

首先你必须生成你的 .class 文件:

javac ./hello.java

javac ./hello.java

This command has generated hello.class file And after you can run your class file ! :)

此命令已生成 hello.class 文件,之后您可以运行您的类文件!:)

java hello

你好

回答by mthmulders

Your .classfile should not reside in /home/scripts/, but in /home/scripts/com/util/. Take a look at this documentthat explains the relation between classpath, packages and directories.

您的.class文件不应驻留在 中/home/scripts/,而应驻留在/home/scripts/com/util/. 看看这个解释类路径、包和目录之间关系的文档

回答by user1651008

down vote

否决票

if you want to run program in current working directory where your class reside.

如果要在类所在的当前工作目录中运行程序。

java gives three options.

java给出了三个选项。

first option

第一个选项

java -cp Tester

java -cp 测试器

Second option for current working directory

当前工作目录的第二个选项

java -cp . Tester

爪哇 -cp 。测试员

Third option export CLASSPATH variable

第三个选项导出 CLASSPATH 变量

export CLASSPATH=$CLASSPATH:. (this is the best one if your directory changes) or

导出类路径=$类路径:。(如果您的目录发生变化,这是最好的)或

export CLASSPATH=$PWD

出口类路径=$PWD

or

或者

export CLASSPATH=

出口类路径=

after that you must sorce the bashrc or bashprofile.

之后,您必须对 bashrc 或 bashprofile 进行检索。

回答by James Liu

We first know javac command work well.

我们首先知道 javac 命令运行良好。

I also met this error,and i have resolved this.Let me share this.

我也遇到了这个错误,我已经解决了。让我分享一下。

First we need to find the parent path of your package in your java codes.

首先我们需要在你的java代码中找到你的包的父路径。

Then cd to that path using java package + fileName should work well at that moment.

然后使用 java package + fileName cd 到该路径在那时应该可以正常工作。

回答by Zinan Xing

I had the exact same issue on windows, and I solved it by adding path "." to both CLASSPATH and PATH, maybe you can try this on Linux as well.

我在 Windows 上遇到了完全相同的问题,我通过添加路径“.”解决了它。到 CLASSPATH 和 PATH,也许你也可以在 Linux 上试试这个。

回答by Chaitanya

Before Specifying the path,ensure you follow these three things meticulously, 1. Close the command prompt window, before specifying the path. 2. When adding path, add bin and semi- colon at the endand 3. If JAVAC command has worked properly, try java -cp class name.

在指定路径之前,请确保您仔细遵循这三件事, 1.在指定路径之前关闭命令提示符窗口。2.添加路径时,在末尾添加 bin 和分号3. 如果 JAVAC 命令正常运行,请尝试 java -cp class name。