Java - 错误:无法找到或加载主类[重复]

问题描述 投票:1回答:1

这个问题在这里已有答案:

我正在尝试从终端运行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

我正在运行此命令来创建一个java class

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

它成功地工作,我得到了Test4.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

还是Error: Could not find or load main class Test4

/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

仍然错误: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
java ubuntu terminal ubuntu-16.04
1个回答
1
投票

您需要将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

希望这可以帮助

© www.soinside.com 2019 - 2024. All rights reserved.