Hi all,
I have recently done an rmi -iiop application and I have compile my code with no errors however when I try to generate the stub class using the rmic -iiop command. I get this error : java.rmi.server.RemoteServer is not a valid remote implementation: has no remote interfaces.
Basically what my code does is when the server is running, the client would enter 2 inputs, a name and choice, the choice is either a "pledge" or "anthem". if pledge is chosen, then the client would retrieve the pledge.txt from the server and prints it out in the client. etc
I would greatly appreciate if anyone can help me out. Thanks a lot
Heres my interface code :
package pledgeanthem;
import java.rmi.Remote;
import java.rmi.RemoteException;
public interface PledgeAnthemServer extends Remote {
public int select(String userName, String choice) throws RemoteException;
public String readOneLine(int key) throws RemoteException;
public boolean close(int key) throws RemoteException;
}
Here's my server code :
package pledgeanthem;
import javax.naming.*;
import java.util.Properties;
public class PledgeAnthemServerApp {
static final String FACTORY = "java.naming.factory.initial";
static final String FACTORY_NAME = "com.sun.jndi.cosnaming.CNCtxFactory";
static final String PROVIDER = "java.naming.provider.url";
static final String PROVIDER_URL = "iiop://localhost:900";
public static void main(String args[]) {
try {
PledgeAnthemServerImpl pledgeAnthemServerImpl = new PledgeAnthemServerImpl();
Properties props = new Properties();
props.put(PledgeAnthemServerApp.FACTORY, PledgeAnthemServerApp.FACTORY_NAME);
props.put(PledgeAnthemServerApp.PROVIDER, PledgeAnthemServerApp.PROVIDER_URL);
InitialContext ic = new InitialContext(props);
ic.rebind("PledgeAnthemServer", pledgeAnthemServerImpl);
System.out.println("PledgeAnthemServer Started \n");
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
Here's my client code :
package pledgeanthem;
import javax.rmi.PortableRemoteObject;
import javax.naming.InitialContext;
import java.util.*;
public class PledgeAnthemClientApp {
static final String FACTORY = "java.naming.factory.initial";
static final String FACTORY_NAME = "com.sun.jndi.cosnaming.CNCtxFactory";
static final String PROVIDER = "java.naming.provider.url";
static String providerUrl = "iiop://";
public static void main(String args[]) {
if(args.length < 3) {
System.out.println("Usage: java pledgeanthem.PledgeAnthemClientApp" + "<serverIpAddress>:<serverPort> <n1> <n2> ");
System.exit(0);
}
Scanner scanner = new Scanner(System.in);
try {
PledgeAnthemClientApp.providerUrl = PledgeAnthemClientApp.providerUrl + args[0];
Properties props = new Properties();
props.put(PledgeAnthemClientApp.FACTORY, PledgeAnthemClientApp.FACTORY_NAME);
props.put(PledgeAnthemClientApp.PROVIDER, PledgeAnthemClientApp.providerUrl);
InitialContext ic = new InitialContext(props);
PledgeAnthemServer pledgeAnthemServer = (PledgeAnthemServer) PortableRemoteObject.narrow(ic.lookup ("PledgeAnthemServer"), PledgeAnthemServer.class);
String name = args[1];
String choice = args[2];
int key = pledgeAnthemServer.select(name, choice);
if((choice.equals("pledge")) || (choice.equals("anthem"))) {
System.out.println("");
System.out.println("File selected at PledgeAnthemServer");
System.out.println("User name = " +name);
System.out.println("Key assigned = "+ key);
System.out.println("");
System.out.println(pledgeAnthemServer.readOneLine( key));
System.out.println("");
if(pledgeAnthemServer.close(key)==true) {
System.out.println("File selected, read and close.");
}
}
else {
key = 999;
System.out.println("");
System.out.println("choice must be 'pledge' or 'anthem'");
}
}
catch (Exception ex) {
ex.printStackTrace();
}
}
}
Here's my implementation class code :
package pledgeanthem;
import java.rmi.RemoteException;
import javax.rmi.PortableRemoteObject;
import java.util.*;
import java.io.*;
public class PledgeAnthemServerImpl extends PortableRemoteObject implements PledgeAnthemServer {
public PledgeAnthemServerImpl()throws java.rmi.RemoteException {
super();
}
//implementation of methods from interface
public int select (String userName, String choice) throws RemoteException {
Random randomGenerator = new Random();
int randomInt = randomGenerator.nextInt(100);
System.out.println("File Opened for "+userName);
System.out.println("Key assigned = "+randomInt);
/*
* To check if choice is either pledge or anthem and copy the corresponding contents of the txt file into
* data.txt
*/
try {
if(choice.equals("pledge")) {
File inputFile = new File("C:\\pledge.txt");
File outputFile = new File("C:\\data.txt");
FileInputStream fis = new FileInputStream(inputFile);
FileOutputStream fos = new FileOutputStream(outputFile);
int c;
while ((c = fis.read()) != -1) {
fos.write(c);
}
fis.close();
fos.close();
}
else if(choice.equals("anthem")) {
File inputFile = new File("C:\\anthem.txt");
File outputFile = new File("C:\\data.txt");
FileInputStream fis = new FileInputStream(inputFile);
FileOutputStream fos = new FileOutputStream(outputFile);
int c;
while ((c = fis.read()) != -1) {
fos.write(c);
}
fis.close();
fos.close();
}
} catch(Exception ex) {
ex.printStackTrace();
}
return randomInt;
} // end of select method
public String readOneLine(int key) throws RemoteException {
String record = new String();
StringBuilder sb = new StringBuilder();
try {
File inputFile = new File("C:\\data.txt");
FileInputStream fstream = new FileInputStream(inputFile);
// Get the object of DataInputStream
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
while((record=br.readLine()) !=null) {
System.out.println("readOneLine() call for key " + key);
sb.append(record).append("\n");
}
return sb.toString();
} catch (Exception ex) {
ex.printStackTrace();
}
return null;
}
public boolean close(int key) throws RemoteException {
System.out.println("readOneLine() call for key "+ key);
System.out.println("File closed for key " + key);
return true;
}
}