Search code examples
javalinuxterminaltwiliojavac

Could not find or load main class called from Linux terminal


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?

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:

[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.)

SendText.java contains 6 imports as follows:

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:

/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:

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:

[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

Solution

  • I did a simple test which replicated the error OP demonstrated:

    Test.java

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

    Compiling: 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:

    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

    com/github/example
    

    And put Test.class in it. Then:

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