Wednesday, 13 September 2017

ubuntu - Java - Error : Could not find or load main class





I am getting a Error while I am trying to run java file from the terminal...



java version...



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 version



javac 1.8.0_181


I have a java file name Test4 with a main.




I am running this command to create a java class



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


and it work successfully , I am getting the Test4.class file



now I am trying to run the java command:




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


and getting



Error: Could not find or load main class Test4


also




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


still Error: Could not find or load main class Test4



also



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



still Error: Could not find or load main class Test4



also



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


still Error: Could not find or load main class Test4




You can find below my 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);
}

}



Inside the folder :



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

Answer



You need to include your Test4 class file to the classpath.



Assuming your Test4.class file is located on /opt/Tests/Test4/src/test4 and the fully qualified name is test4.Test4 then you should do something like:




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



Note that the directory of the terminal is /opt/Tests/Test4/src and not /opt/Tests/Test4/src/test4



Hope this helps


No comments:

Post a Comment

casting - Why wasn't Tobey Maguire in The Amazing Spider-Man? - Movies & TV

In the Spider-Man franchise, Tobey Maguire is an outstanding performer as a Spider-Man and also reprised his role in the sequels Spider-Man...