Unable to locate or load main class

Asked

Viewed 1,274 times

1

I recently formatted my computer and am having trouble running my java programs.

I am using linux Mint and the settings are as follows:

java -version

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

echo $PATH

/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/usr/lib/jvm/java-8-oracle/bin:/usr/lib/jvm/java-8-oracle/db/bin:/usr/lib/jvm/java-8-oracle/jre/bin

echo $JAVA_HOME

/usr/lib/jvm/java-8-oracle

and $CLASSPATH is empty.

I can compile the programs nicely and generate the . class files, but when I run the java Server command I always get the following error:

Error: Unable to find or load main class Server

What should I do to be able to run my programs through the terminal?

The command I use in the shell is:

 ~$javac *.java
 ~$java Servidor

The class I’m trying to execute is as follows :

Java server.

    package helloWorld;

    import java.rmi.Remote;
    import java.rmi.registry.Registry;
    import java.rmi.registry.LocateRegistry;
    import java.rmi.RemoteException;
    import java.rmi.server.UnicastRemoteObject;

    public class Servidor implements InterfaceObjeto{

    // A chamada do construtor serve para criar os stubs
    public Servidor() {} 

    public String sayHello(){
        return "Hello, World RMI";
    }

    public static void main(String[] args) {

        try{

            // cria e exporta o objeto remoto
            Servidor objeto = new Servidor();
            InterfaceObjeto stub = (InterfaceObjeto) UnicastRemoteObject.exportObject( objeto, 0);

            // registra o objeto com rmi registry
            Registry registro = LocateRegistry.getRegistry();
            registro.bind("interfaceObjeto", stub);

            System.err.println("Servidor pronto");

        }   catch (Exception e) {
            System.err.println("Server exception: " + e.toString());
            e.printStackTrace();
        }


    }
}

Interfaceobjeto.java

package helloWorld;

import java.rmi.Remote;
import java.rmi.RemoteException;

public interface InterfaceObjeto extends Remote{
    public String sayHello() throws RemoteException;
}

Java client.

package helloWorld;

import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;

public class Cliente {

    private Cliente(){}

    public static void main(String[] args) {

        String host = (args.length < 1) ? null : args[0];

        try{

            Registry registro = LocateRegistry.getRegistry(host);
            InterfaceObjeto stub = (InterfaceObjeto)     registro.lookup("InterfaceObjeto");
            String resposta = stub.sayHello();
            System.out.println("resposta: " + resposta);

        } catch (Exception e) {
                System.err.println("Client exception: " +  e.toString());
                e.printStackTrace();
            }
    }
}

When I run the file through netbeans I get the following error:

Server exception: java.rmi.ServerException: RemoteException occurred in server thread; nested exception is: 
java.rmi.UnmarshalException: error unmarshalling arguments; nested exception is: 
java.lang.ClassNotFoundException: helloWorld.InterfaceObjeto
  • added there :)

  • There is no import for InterfaceObjeto. Check if this class is being compiled as well. If it is not found, your code will not run.

  • But Interfaceobject is in the same package as the Server class, yet it is necessary to import?

  • No. So the problem is not in this class. Please add all the classes involved, such as the interface cited.

  • already added! It may be problem in the classes, I’m learning to use rmi yet, but I don’t see where the problem is

  • surround another program that does not use RMI here by shell using the javac and java commands and they are working normally...

  • http://stackoverflow.com/a/9542464/5524514

  • Please post the command line in which you run your app.

  • already put. But there is nothing magical eh only a javac and a java ...

  • Have you looked at the link I posted?

  • Yes.. I made the Unicastremoteobject extender Server class and it worked, now I can run through netbeans! But on the command line nothing yet..

  • Surely something is missing from your classpath. You can get the command line that netbeans is using to run your app?

Show 7 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.