当我尝试从终端运行 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
我有一个java文件名Test4
与 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
您可以在下面找到我的类(class):
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-错误 : Could not find or load main class,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52332838/