I am using javac
to compile a java program at the CentOS 7 terminal. The program is very simple and is being used for some testing. But it is throwing a could not find or load main class
error as shown below. How can I resolve this error?
我正在使用javac在CentOS 7终端上编译java程序。这个程序非常简单,正在用于一些测试。但它抛出了一个无法找到或加载主类错误,如下所示。如何解决这个错误?
Here is the background:
这是背景:
The java program is located in /home/user/javacode/
, and the two jar files it uses as dependencies are in /home/user/javacode/dependencies
. The terminal commands and responses from trying to compile and run it are:
java程序位于/home/user/javacode/,它使用的两个jar文件作为依赖项在/home/user/javacode/dependencies中。试图编译和运行终端的命令和响应是:
[user@domain javacode]$ javac -cp .:/home/user/javacode/dependencies/twilio-java-sdk-3.4.5.jar:/home/user/javacode/dependencies/httpcore-4.1.2.jar SendText.java
[user@domain javacode]$ java -cp .:/home/user/javacode/dependencies/twilio-java-sdk-3.4.5.jar:/home/user/javacode/dependencies/httpcore-4.1.2.jar mainpackage.SendText xxxxxxxxxx HelloThere
Error: Could not find or load main class name.of.package.SendText
Is the problem with the compilation syntax, or with the calling syntax? Note that xxxxxxxxxx
and HelloThere
are two args
to be sent to the program when it is called. xxxxxxxxxx
is the phone number to which a text will be sent, and HelloThere
is the message. (The code is tested and does send a text if compiled and run correctly.)
编译语法或调用语法有问题吗?注意,当程序被调用时,将向程序发送两个args。xxxxxxxx是发送文本的电话号码,HelloThere是消息。(代码经过测试,如果编译并正确运行,则会发送文本。)
SendText.java
contains 6 imports as follows:
SendText。java包含6个导入:
import com.twilio.sdk.TwilioRestClient;
import com.twilio.sdk.TwilioRestException;
import com.twilio.sdk.resource.factory.MessageFactory;
import com.twilio.sdk.resource.instance.Message;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
I checked, and all of the 6 imports are located in the following two jars, which I also specified in the commands above:
我检查了,所有6个导入都位于以下两个jar中,我在上面的命令中也指定了这两个jar:
/home/user/javacode/dependencies/twilio-java-sdk-3.4.5.jar
/home/user/javacode/dependencies/httpcore-4.1.2.jar
Just to prove that there is a main method in SendText.java
, the results of nano SendText.java
are:
只是为了证明SendText中有一个主方法。java,纳米SendText的结果。java是:
package name.of.package;
import com.twilio.sdk.TwilioRestClient;
import com.twilio.sdk.TwilioRestException;
import com.twilio.sdk.resource.factory.MessageFactory;
import com.twilio.sdk.resource.instance.Message;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import java.util.ArrayList;
import java.util.List;
public class SendText {
public static final String ACCOUNT_SID = "arealsid";
public static final String AUTH_TOKEN = "arealtoken";
public static void main(String[] args) throws TwilioRestException {
TwilioRestClient client = new TwilioRestClient(ACCOUNT_SID, AUTH_TOKEN);
String to = args[0];
String body = args[1];
// Build a filter for the MessageList
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("Body", body));
params.add(new BasicNameValuePair("To", to));
params.add(new BasicNameValuePair("From", "+11234567654"));
MessageFactory messageFactory = client.getAccount().getMessageFactory();
Message message = messageFactory.create(params);
System.out.println(message.getSid());
}
}
Per @Tyler's comments, I typed the following and got the following responses:
在@Tyler的评论中,我输入了以下内容,并得到如下回复:
[user@domain javacode]$ ls -al
total 12
drwxrwxr-x 3 user user 66 Aug 21 21:19 .
drwxr-xr-x. 6 user user 4096 Aug 21 21:20 ..
drwxrwxr-x 2 user user 63 Aug 21 21:24 dependencies
-rw-rw-r-- 1 user user 1495 Aug 21 21:27 SendText.class
-rw-r--r-- 1 user user 1313 Aug 21 19:28 SendText.java
[user@domain javacode]$ java -version
java version "1.7.0_75"
OpenJDK Runtime Environment (rhel-2.5.4.2.el7_0-x86_64 u75-b13)
OpenJDK 64-Bit Server VM (build 24.75-b04, mixed mode)
[user@domain javacode]$ javac -version
javac 1.7.0_75
[user@domain javacode]$ java SendText
Exception in thread "main" java.lang.NoClassDefFoundError: SendText (wrong name: name.of.package/SendText)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:800)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:482)
[user@domain javacode]$ java name.of.package.SendText
Error: Could not find or load main class name.of.package.SendText
2 个解决方案
#1
2
I did a simple test which replicated the error OP demonstrated:
我做了一个简单的测试,复制了OP演示的错误:
Test.java
Test.java
package com.github.example;
public class Test{
public static void main(String args[]){
System.out.println("hello world");
}
}
Compiling: javac Test.java
编译:javac Test.java
Running like OP:
运行像人事处:
java com.github.example.Test
Error: Could not find or load main class com.github.example.Test
Running like OP without package name:
像OP一样运行,没有包名:
java Test
Exception in thread "main" java.lang.NoClassDefFoundError: Test (wrong name: com/github/example/Test)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:800)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:482)
As suggested by @Javy, I created the directory structure
正如@Javy所建议的,我创建了目录结构。
com/github/example
And put Test.class in it. Then:
并将测试。类。然后:
mv Test.class com/github/example/
java com.github.example.Test
hello world
#2
1
you shoud make directory according your package name
您应该根据包名创建目录
for example
例如
package com.peng.test;
public class A {
public static void main(String []args) {
System.out.println("test");
}
}
The current directory structure is as the following
当前目录结构如下所示
TestJ
--A.java
then you should make directory
然后创建目录
mkdir bin
then compile the A.java
using the following cmd
然后编译。使用以下cmd的java
javac -d bin A.java
now the directory like the following
现在,目录如下。
TestJ
--A.java
--bin
--com
--peng
--test
--A.class
then
然后
cd bin
then
然后
java com.peng.test.A
#1
2
I did a simple test which replicated the error OP demonstrated:
我做了一个简单的测试,复制了OP演示的错误:
Test.java
Test.java
package com.github.example;
public class Test{
public static void main(String args[]){
System.out.println("hello world");
}
}
Compiling: javac Test.java
编译:javac Test.java
Running like OP:
运行像人事处:
java com.github.example.Test
Error: Could not find or load main class com.github.example.Test
Running like OP without package name:
像OP一样运行,没有包名:
java Test
Exception in thread "main" java.lang.NoClassDefFoundError: Test (wrong name: com/github/example/Test)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:800)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:482)
As suggested by @Javy, I created the directory structure
正如@Javy所建议的,我创建了目录结构。
com/github/example
And put Test.class in it. Then:
并将测试。类。然后:
mv Test.class com/github/example/
java com.github.example.Test
hello world
#2
1
you shoud make directory according your package name
您应该根据包名创建目录
for example
例如
package com.peng.test;
public class A {
public static void main(String []args) {
System.out.println("test");
}
}
The current directory structure is as the following
当前目录结构如下所示
TestJ
--A.java
then you should make directory
然后创建目录
mkdir bin
then compile the A.java
using the following cmd
然后编译。使用以下cmd的java
javac -d bin A.java
now the directory like the following
现在,目录如下。
TestJ
--A.java
--bin
--com
--peng
--test
--A.class
then
然后
cd bin
then
然后
java com.peng.test.A