无法找到或加载从linux终端调用的主类

时间:2015-08-22 01:58:36

标签: java linux terminal twilio javac

我正在使用javac在CentOS 7终端上编译java程序。该程序非常简单,用于一些测试。但它会引发could not find or load main class错误,如下所示。我该如何解决这个错误?

以下是背景资料:

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

编译语法或调用语法有问题吗?请注意,xxxxxxxxxxHelloThere是要发送到程序的两个args什么时候叫它。 xxxxxxxxxx是文本将被发送到的电话号码,HelloThere就是消息。 (代码经过测试,如果编译并正确运行,会发送文本。)

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;  

我检查了,所有6个进口都位于以下两个罐子里,我在上面的命令中也指定了这些罐子:

/home/user/javacode/dependencies/twilio-java-sdk-3.4.5.jar
/home/user/javacode/dependencies/httpcore-4.1.2.jar   

为了证明SendText.java中有一个主要方法,nano 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的评论,我输入以下内容并得到以下答复:

[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 个答案:

答案 0 :(得分:2)

我做了一个简单的测试,复制了OP演示的错误:

Test.java

package com.github.example;

public class Test{
  public static void main(String args[]){
    System.out.println("hello world");
  }
}

编译:     javac Test.java

像OP一样跑:

java com.github.example.Test
Error: Could not find or load main class com.github.example.Test

像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)

根据@Javy的建议,我创建了目录结构

com/github/example

将Test.class放入其中。然后:

mv Test.class com/github/example/
java com.github.example.Test
hello world

答案 1 :(得分:1)

根据您的package name

创建目录

例如

package com.peng.test;
public class A {
    public static void main(String []args) {
        System.out.println("test");
    }
}

当前目录结构如下

TestJ
   --A.java

然后你应该创建目录

mkdir bin

然后使用以下cmd编译A.java

javac -d bin A.java

现在的目录如下

TestJ
   --A.java
   --bin
     --com
       --peng
         --test
           --A.class

然后

cd bin

然后

java com.peng.test.A