Java Error:cannot find or load main class java - java

I am trying to setup a simple rmi server
import java.net.MalformedURLException;
import java.rmi.*;
import java.rmi.server.*;
public class EmployeeServer extends UnicastRemoteObject implements Employee{
private String name,id;
private int salary;
private static final long serialVersionUID=1L;
public EmployeeServer(String n,String i,int s) throws RemoteException {
super();
name=n;
id=i;
salary=s;
}
public String getName() throws RemoteException {
return name;
}
public String getID() throws RemoteException {
return id;
}
public int getSalary() throws RemoteException {
return salary;
}
public int augmentSalary(int howMuch) throws RemoteException {
if(howMuch>0){
salary+=howMuch;
}
return salary;
}
public static void main(String[] args) throws RemoteException, MalformedURLException, AlreadyBoundException {
System.setSecurityManager(new SecurityManager());
EmployeeServer server=new EmployeeServer("Red","0123",5000);
Naming.rebind("Red", server);
System.out.println("Server Ready!");
}
}
With this code alone i was getting the error
Exception in thread "main" java.security.AccessControlException: access denied ("java.net.SocketPermission" "127.0.0.1:1099" "connect,resolve")
After A LOT of googling i finally wrote my policy file
grant codeBase "file:/Utenti/Super/workspacenew/PDEsercizioStubSkeleton2.0/bin/EmployeeServer.class"
{
permission java.net.SocketPermission "127.0.0.1:1099", "connect, resolve";
};
And put it in my project folder "PDEsercizioStubSkeleton2.0".
Now i was trying to include this policy file in my jvm run configurations putting this in the VM arguments:
java -Djava.security.manager -Djava.security.policy=server.policy
But then it gives me the "Error: could not find or load the main class java".
I found out it just checks the first word wrote, because i modified "java" with "jaa" and got the same error with the misspelled name. What am i doing wrong?

Related

com.sun.proxy.$Proxy0 cannot be cast to

I have the following RMI Connection code which returns a com.sun.proxy.$Proxy0 cannot be cast to Client.AdditionInterface error. I created two separate packages Client & Server and putted the Interface in both of them. Here is my complete code:
package Serveur;
import java.rmi.*;
import java.rmi.registry.LocateRegistry;
public class AdditionServer {
public static void main (String[] argv) {
try {
LocateRegistry.createRegistry(1099);
Addition Hello = new Addition();
Naming.rebind("rmi://"+java.net.InetAddress.getLocalHost().getHostName()+"/ABC", Hello);
System.out.println("Addition Server is ready.");
}
catch (Exception e) {
System.out.println("Addition Server failed: " + e);
}
}
}
//////////////////////////////////////
package Serveur;
import java.rmi.*;
import java.rmi.server.*;
public class Addition extends UnicastRemoteObject implements AdditionInterface {
public Addition () throws RemoteException {
super();
}
#Override
public int add(int a, int b) throws RemoteException {
int result=a+b;
return result;
}
#Override
public String aff(int a, int b) throws RemoteException {
return String.valueOf(add(a, b));
}
}
////////////////////////////
package Client;
import java.rmi.Remote;
import java.rmi.RemoteException;
public interface AdditionInterface extends Remote {
public int add(int a,int b) throws RemoteException;
public String aff(int a, int b) throws RemoteException;
}
////////////////////////////
package Client;
import java.rmi.*;
public class AdditionClient {
public static void main (String[] args) throws Exception {
AdditionInterface add = (AdditionInterface) Naming.lookup("rmi://"+java.net.InetAddress.getLocalHost().getHostName()+"/ABC");
System.out.println("Result is :"+add.add(9, 10));
System.out.println(add.aff(26, 45));
}
}
Any help please? Thank you.
As in both a Client.AdditionInterface for the client and a Server.AdditionInterface for the server? Effectively the error is saying Server.AdditionInterface cannot be cast to Client.AdditionInterface.
With RMI you must have the same interface class on both client and server, but then clearly the server implementation class (AdditionServer) just on the server. The interface could be in a shared package (e.g. common.AdditionInterface ).

Text doesn't appear on GUI while using RMI

My aim is to make a simple chat program. I'm new at RMI. What I've got so far is that the server works. I start it. Then I start the client, it transfers the strings to the server through RMI. But then it doesn't appear on the GUI I made. That's where my problem lies.
My project structure
My StartClient class. I created a chatClient, and put the chatServer stub as parameter.
public StartClient() throws RemoteException, NotBoundException, MalformedURLException {
chatServer = (ChatServer) Naming.lookup("rmi://localhost:1099/chatServer");
}
private void run() throws RemoteException, MalformedURLException, NotBoundException {
ChatClientImpl chatClient1 = new ChatClientImpl(chatServer, "ikke");
new ChatFrame(chatClient1);
ChatClientImpl chatClient2 = new ChatClientImpl(chatServer, "bla");
new ChatFrame(chatClient2);
}
public static void main(String[] args) throws RemoteException, NotBoundException, MalformedURLException {
StartClient start = new StartClient();
start.run();
}
In the ChatClientImpl constructor I use the remote method register.
public ChatClientImpl(ChatServer chatServer, String name) throws MalformedURLException, NotBoundException, RemoteException {
this.chatServer = chatServer;
this.name = name;
chatServer.register(this);
}
Now we're in the ChatServerImpl class, in the REGISTER method. I add the client to an ArrayList of clients. Then I use the method SENT to display the text. It calls the RECEIVE method that each client object has.
public class ChatServerImpl extends UnicastRemoteObject implements ChatServer {
private List<ChatClient> clients;
public ChatServerImpl() throws RemoteException {
this.clients = new ArrayList<ChatClient>();
}
public void register(ChatClientImpl client) throws RemoteException {
clients.add(client);
send("server", client.getName() + " has entered the room");
}
public void unregister(ChatClientImpl client) throws RemoteException {
clients.remove(client);
send("server", client.getName() + " has left the room");
}
public void send(String name, String message) throws RemoteException {
for(ChatClient client : clients) {
client.receive(name + ": " + message);
}
}
}
This is where things go wrong. The textReceiver is ALWAYS null. (textReceiver is attribute/field of the client object.)
public void receive(String message) {
if (textReceiver == null) return;
textReceiver.receive(message);
}
The ArrayList of clients are server-side and all the clients in there all have their textReceivers set on null. If you look back at StartClient there's an important line. The new ChatFrame(chatClient). In the ChatFrame's constructor is where I set the textReceiver.
public ChatFrame(ChatClientImpl chatClient) {
this.chatClient = chatClient;
chatClient.setTextReceiver(this);
String name = chatClient.getName();
setTitle("Chat: " + name);
createComponents(name);
layoutComponents();
addListeners();
setSize(300, 300);
setVisible(true);
}
This project works when I don't use RMI and they're in one package but once I separate them into client-server this problem arose. How do I communicate between them? Server-side I have an (irrelevant?) list of ChatClients that don't influence anything even though the text arrives.
Do I use RMI for every separate ChatClient and make the ChatServer connect with it and send the text like that? Seems very complicated to me. How do I go about this?
EDIT:
ChatClientImpl class
public class ChatClientImpl implements ChatClient, Serializable {
private ChatServer chatServer;
private TextReceiver textReceiver;
private String name;
public ChatClientImpl(ChatServer chatServer, String name) throws MalformedURLException, NotBoundException, RemoteException {
this.chatServer = chatServer;
this.name = name;
chatServer.register(this);
}
public String getName() {
return name;
}
public void send(String message) throws RemoteException {
chatServer.send(name, message);
}
public void receive(String message) {
if (textReceiver == null) return;
textReceiver.receive(message);
}
public void setTextReceiver(TextReceiver textReceiver) {
this.textReceiver = textReceiver;
}
public void unregister() throws RemoteException {
chatServer.unregister(this);
}
}
Your ChatClientImpl class isn't an exported remote object, so it will be serialized to the server, and execute there. And because register() happens during construction, it will be serialized before the setReceiverTextReceiver() method is called. So, the corresponding field will be null. At the server. This is not what you want and it is also not where you want it.
So, make it extend UnicastRemoteObject and implement your ChatClient (presumed) remote interface. If you have problems with doing that, solve them. Don't just mess around with things arbitrarily. And it should not implement Serializable.
NB The signature of register() should be register(ChatClient client). Nothing to do with the ChatClientImpl class. Ditto for unregister().

Database object is not working with a working RMI package

I have 2 projects. One works fine in ever department. I downloaded and modified it to better understand it. The 2nd one is a project in development phase.
Now, both these projects have almost exactly the same RMI package, which works fine in the first project, but not in the 2nd.
My test classes in each package are essentially identical as well.
The main difference is what objects there are attempting to access, which are both interfaces in a database package.
Now, the database package in the 2nd project otherwise works absolutely fine, it just wont work with the RMI.
In short:
database package works fine
RMI package works fine
RMI package and database together does not work fine.
Here is my DBInterface
public interface DB extends Remote {
public String[] read(int recNo) throws RecordNotFoundException;
public void update(int recNo, String[] data, long lockCookie)
throws RecordNotFoundException, SecurityException, IOException;
public void delete(int recNo, long lockCookie)
throws RecordNotFoundException, SecurityException, IOException;
public int[] find(String[] criteria);
public int create(String[] data) throws DuplicateKeyException, IOException;
public long lock(int recNo) throws RecordNotFoundException;
public void unlock(int recNo, long cookie)
throws RecordNotFoundException, SecurityException;
}
and here is my RMIInterface
public interface RMIInterface extends Remote{
public DB getClient() throws RemoteException;
}
My RMIImplementation
public class RMIImplementation extends UnicastRemoteObject
implements RMIInterface {
private static String dbLocation = null;
private DB a;
public RMIImplementation() throws RemoteException{
}
public RMIImplementation(String dbLocation) throws RemoteException{
System.out.println(dbLocation);
this.dbLocation = dbLocation;
}
public static DB getRemote(String hostname, String port)
throws RemoteException {
String url = "rmi://" + hostname + ":" + port + "/DvdMediator";
try {
RMIInterface factory
= (RMIInterface) Naming.lookup(url);
// at this point factory equals Proxy[RMIInterface,................etc
// i want the return to equal Proxy[DB,..............etc
return (DB) factory.getClient();
} catch (NotBoundException e) {
throw new RemoteException("Dvd Mediator not registered: ", e);
}
catch (java.net.MalformedURLException e) {
throw new RemoteException("cannot connect to " + hostname, e);
}
}
public DB getClient() throws RemoteException {
try {
a = new ContractorDatabase(dbLocation);
}
catch(Exception e){
System.out.println("NewClass exception: " + e.toString());
}
return a;
}
And my the RMI registry
public class RegDvdDatabase {
private RegDvdDatabase() {
}
public static void register()
throws RemoteException {
register(".", java.rmi.registry.Registry.REGISTRY_PORT);
}
public static void register(String dbLocation, int rmiPort)
throws RemoteException {
Registry r = java.rmi.registry.LocateRegistry.createRegistry(rmiPort);
r.rebind("DvdMediator", new RMIImplementation(dbLocation));
}
}
Getting these two to work together throws a
Exception in thread "main" java.lang.ClassCastException: com.sun.proxy.$Proxy0 cannot be cast to sjdproject.remote.RMIImplementation
Can u please help me find the database issue that prevents it from working.
You must cast it to the remote interface.
EDIT The Registry reference r in your server code must be static. I can't see any good reason for locating the client lookup code inside the implementation class. That class should only exist at the server, not the client.
If you dont have a debugger, I would suggest using reflection on the provided object and see which interfaces it implements. It appears to be a proxy object, so must implement some interfaces.
for(Class clazz : factory.getClass().getInterfaces()) {
System.out.println(clazz.getSimpleName());
}
My suspicion with multiple deployments is of course the jvm version and the classpath. Can you verify that they match?

how to call android activity from standard java code?

This is my sample test project which tests android calculator using robotium.
I want to create jar file of this project but while creating it, it shows error as below:
"Error: Could not find or load main class TestMain".
I think its showing this error becouse there is no main class in that i.e. it couldn't find "public static void main(String args[])". what should i do to overcome this problem?
package com.testcalculator;
import com.jayway.android.robotium.solo.Solo;
import android.test.ActivityInstrumentationTestCase2;
#SuppressWarnings("unchecked")
public class TestCal extends ActivityInstrumentationTestCase2
{
private static final String TARGET_PACKAGE_ID="com.calculator";
private static final String LAUNCHER_ACTIVITY_FULL_CLASSNAME="com.calculator.Main";
private static Class launcherActivityClass;
static
{
try
{
launcherActivityClass=Class.forName(LAUNCHER_ACTIVITY_FULL_CLASSNAME);
}
catch (ClassNotFoundException e)
{
throw new RuntimeException(e);
}
}
public TestCal()throws ClassNotFoundException
{
super(launcherActivityClass);
}
private Solo solo;
#Override
protected void setUp() throws Exception
{
solo = new Solo(getInstrumentation(),getActivity());
}
public void testDisplayBlackBox()
{
solo.enterText(0, "10");
solo.enterText(1, "20");
solo.clickOnButton("Multiply");
assertTrue(solo.searchText("200"));
}
#Override
public void tearDown() throws Exception
{
solo.finishOpenedActivities();
}
}

need help to run RMI Registry

I'm implementing a simple RMI Server Client program in JAVA. I'm new to this actually. i have four java files.
Stack.java
import java.rmi.*;
public interface Stack extends Remote{
public void push(int p) throws RemoteException;
public int pop() throws RemoteException;
}
StackImp.java
import java.rmi.*;
import java.rmi.server.*;
public class StackImp extends UnicastRemoteObject implements Stack{
private int tos, data[], size;
public StackImp()throws RemoteException{
super();
}
public StackImp(int s)throws RemoteException{
super();
size = s;
data = new int[size];
tos=-1;
}
public void push(int p)throws RemoteException{
tos++;
data[tos]=p;
}
public int pop()throws RemoteException{
int temp = data[tos];
tos--;
return temp;
}
}
RMIServer.java
import java.rmi.*;
import java.io.*;
public class RMIServer{
public static void main(String[] argv) throws Exception{
StackImp s = new StackImp(10);
Naming.rebind("rmi://localhost:2000/xyz", s);
System.out.println("RMI Server ready....");
System.out.println("Waiting for Request...");
}
}
RMIClient.java
import java.rmi.*;
public class RMIClient{
public static void main(String[] argv)throws Exception{
Stack s = (Stack)Naming.lookup("rmi://localhost:2000/xyz");
s.push(25);
System.out.println("Push: "+s.push());
}
}
I'm using JDK1.5. The sequence in which i compiled the files is, first i compiled Stack.java then i compiled StackImp.java then i used this command rmic StackImp this all was successful. But when i tried to run the registry this way rmiregistery 2000, command prompt took too long. Nothing happened. I'm doing this all at my home PC. And this PC is not on the network. Please suggest me what to do to successfully work with this program.
command prompt took too long. Nothing happened.
Nothing is supposed to happen - the registry is running, and you can now start your server from another command prompt.
Alternatively, if you're only running the one RMI server process on this machine you can run the registry in the same process as the RMI server:
import java.rmi.*;
import java.rmi.registry.*;
import java.io.*;
public class RMIServer{
public static void main(String[] argv) throws Exception{
StackImp s = new StackImp(10);
Registry reg = LocateRegistry.createRegistry(2000);
reg.rebind("xyz", s);
System.out.println("RMI Server ready....");
System.out.println("Waiting for Request...");
}
}
This way you don't need a separate rmiregistry command, just run the server (which includes the registry) and then the client (which talks to the registry that is running in the server process).

Categories