This question already has answers here:
What does “Could not find or load main class” mean?
                                
                                    (46个答案)
                                
                        
                                2年前关闭。
            
                    
我在尝试从终端运行Java文件时遇到错误...

Java版本...

java version "1.8.0_181"
Java(TM) SE Runtime Environment (build 1.8.0_181-b13)
Java HotSpot(TM) 64-Bit Server VM (build 25.181-b13, mixed mode)


javac版本

javac 1.8.0_181


我有一个Test4的Java文件名。

我正在运行此命令来创建main

/opt/Tests/Test4/src/test4> javac -cp "/opt/glassfish5/glassfish/lib/*"
Test4.java


并且工作成功,我正在获取java class文件

现在我正在尝试运行java命令:

/opt/Tests/Test4/src/test4> java -cp "/opt/glassfish5/glassfish/lib/*" Test4


并得到

Error: Could not find or load main class Test4




/opt/Tests/Test4/src> java -cp "/opt/glassfish5/glassfish/lib/*" test4.Test4


Test4.class



/opt/Tests/Test4/src/test4> java -cp "/opt/glassfish5/glassfish/lib/*"
Test4.class


仍然错误:找不到或加载主类Test4



/opt/Tests/Test4/src> java -cp "/opt/glassfish5/glassfish/lib/*"
test4.Test4.class


仍然错误:Error: Could not find or load main class Test4

您可以在我的课程下面找到:

package test4;



import java.util.Hashtable;
import java.util.Map;
import java.util.Properties;
import javax.jms.Connection;
import javax.jms.Queue;
import javax.jms.ConnectionFactory;
import javax.jms.JMSContext;
import javax.jms.JMSException;
import javax.jms.JMSProducer;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

/**
*
* @author zion
*/
public class Test4 {

/**
 * @param args the command line arguments
 */
public static void main(String[] args) throws NamingException {

    Context initialContext = Test4.getInitialContext();

    ConnectionFactory connectionFactory = (ConnectionFactory)initialContext.lookup("jms/connection");

    JMSContext jMSContext =  connectionFactory.createContext();
    Queue myQueue = (Queue)initialContext.lookup("jms/myQueue");
    JMSProducer jMSProducer = jMSContext.createProducer();
    jMSProducer.send(myQueue, "Hi,Zion");
    System.out.println("work work work wrok wrok");
}



 public static Context getInitialContext() throws NamingException{
    Hashtable env = new Hashtable();
    env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.enterprise.naming.impl.SerialInitContextFactory");
    env.put(Context.STATE_FACTORIES, "com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl");
    env.put(Context.URL_PKG_PREFIXES, "com.sun.enterprise.naming");
    env.put("org.omg.CORBA.ORBInitialHost", "IP");
    env.put("org.omg.CORBA.ORBInitialPort", "Port");

    return new InitialContext(env);
}

}


在文件夹内:

/opt/Tests/Test4/src/test4# ls
Test4.class  Test4.java

最佳答案

您需要将您的Test4类文件包括到类路径中。

假设您的Test4.class文件位于/opt/Tests/Test4/src/test4上,并且完全限定名称为test4.Test4,那么您应该执行以下操作:

/opt/Tests/Test4/src> java -cp "/opt/glassfish5/glassfish/lib/*":"." test4.Test4

请注意,终端的目录是/ opt / Tests / Test4 / src,而不是/ opt / Tests / Test4 / src / test4

希望这可以帮助

关于java - Java-错误:找不到或加载主类,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/52332838/

10-09 09:40