java.rmi.server.ExportException: Port already in use: 1099; - java

I am writing a Java RMI program to calculate the factorial of a number.
This is the interface:
import java.rmi.Remote;
import java.rmi.RemoteException;
public interface FactorialRMI extends Remote{
public void setNumber(int val) throws RemoteException;
public int calculateFactorial() throws RemoteException;
}
Then this is the class that implements the interface:
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
public class FactorialRMIImpl extends UnicastRemoteObject implements FactorialRMI{
private int number;
public FactorialRMIImpl(String name) throws RemoteException {
super();
try {
Naming.rebind("myFactorial",this);
}catch(Exception e) {
System.out.println("Exception: "+e.getMessage());
}
}
#Override
public void setNumber(int val) throws RemoteException {
this.number=val;
}
#Override
public int calculateFactorial() throws RemoteException {
int p=1;
for(int i=1;i<=this.number;i++)
p=p*i;
return p;
}
}
Here is the server code:
public class FactorialRMIServer {
public static void main(String [] args) {
try {
FactorialRMIImpl myFactorial=new FactorialRMIImpl("myFactorial");
System.out.println("Server is ready...");
}catch(Exception e) {
System.out.println("Exception: "+e.getMessage());
}
}
}
And here is the client code:
import java.rmi.Naming;
import java.util.*;
public class FactorialRMIClient {
public static void main(String [] args) {
try {
FactorialRMI myFact=(FactorialRMI)Naming.lookup("myFactorial");
Scanner scan=new Scanner(System.in);
System.out.println("Enter a number to calculate its factorial: ");
int n=scan.nextInt();
myFact.setNumber(n);
System.out.println("The factorial of this number is: "+myFact.calculateFactorial());
}catch(Exception e) {
System.out.println("Exception: "+e.getMessage());
}
}
}
I am not using eclipse.
i am required to run this program in the bin folder of the jdk.
i am using jdk1.8
to compile the java file i put these file in the bin folder and run the command:
javac *.java
then i run the commands
rmic FactorialRMIImpl
start rmiregistry
start java FactorialRMIServer
start java FactorialRMIClient
in the last 2 command i get this error:
Exception: Connection refused to host: 127.0.0.1; nested exception is:
java.net.ConnectException: Connection refused: connect
The command start rmiregistry gives me an error:
java.rmi.server.ExportException: Port already in use: 1099; nested exception is:
java.net.BindException: Address already in use: NET_Bind
at java.rmi/sun.rmi.transport.tcp.TCPTransport.listen(TCPTransport.java:335)
at java.rmi/sun.rmi.transport.tcp.TCPTransport.exportObject(TCPTransport.java:243)
at java.rmi/sun.rmi.transport.tcp.TCPEndpoint.exportObject(TCPEndpoint.java:411)
at java.rmi/sun.rmi.transport.LiveRef.exportObject(LiveRef.java:147)
at java.rmi/sun.rmi.server.UnicastServerRef.exportObject(UnicastServerRef.java:234)
at java.rmi/sun.rmi.registry.RegistryImpl.setup(RegistryImpl.java:220)
at java.rmi/sun.rmi.registry.RegistryImpl$2.run(RegistryImpl.java:196)
at java.rmi/sun.rmi.registry.RegistryImpl$2.run(RegistryImpl.java:193)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:689)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:873)
at java.rmi/sun.rmi.registry.RegistryImpl.<init>(RegistryImpl.java:193)
at java.rmi/sun.rmi.registry.RegistryImpl$5.run(RegistryImpl.java:531)
at java.rmi/sun.rmi.registry.RegistryImpl$5.run(RegistryImpl.java:529)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:689)
at java.rmi/sun.rmi.registry.RegistryImpl.createRegistry(RegistryImpl.java:528)
at java.rmi/sun.rmi.registry.RegistryImpl.main(RegistryImpl.java:551)
Caused by: java.net.BindException: Address already in use: NET_Bind
at java.base/java.net.PlainSocketImpl.bind0(Native Method)
at java.base/java.net.PlainSocketImpl.socketBind(PlainSocketImpl.java:132)
at java.base/java.net.AbstractPlainSocketImpl.bind(AbstractPlainSocketImpl.java:436)
at java.base/java.net.ServerSocket.bind(ServerSocket.java:386)
at java.base/java.net.ServerSocket.<init>(ServerSocket.java:248)
at java.base/java.net.ServerSocket.<init>(ServerSocket.java:140)
at java.rmi/sun.rmi.transport.tcp.TCPDirectSocketFactory.createServerSocket(TCPDirectSocketFactory.java:45)
at java.rmi/sun.rmi.transport.tcp.TCPEndpoint.newServerSocket(TCPEndpoint.java:666)
at java.rmi/sun.rmi.transport.tcp.TCPTransport.listen(TCPTransport.java:324)
... 15 more
How can I solve this?

if you are running on windows, you can find the process running on port and kill that process using below command, so that port will be freed
netstat -ano | findstr :1099
taskkill /pid "EnterProcessIdHere" /F
For linux
lsof -i :1099
kill EnterProcessIdHere

Related

ERROR CANNOT FIND SYMBOL when compiling a Java file

I want to create a RMI application. I set the java path in the System Variables and started with these steps
javac Hello.java
javac HelloImpl.java
javac HelloServer.java
javac HelloClient.java
start rmiregistry
start java -Djava.security.policy=policy Server
start java -Djava.security.policy=policy Client
But in the second step I have this problem
C:\Users\HP\eclipse\SimpleRMIExample\src>javac Hello.java
C:\Users\HP\eclipse\SimpleRMIExample\src>javac HelloImpl.java
HelloImpl.java:4: error: cannot find symbol
public class HelloImpl extends UnicastRemoteObject implements Hello {
^
symbol: class Hello
1 error
Code
//Hello.java
import java.rmi.*;
public interface Hello extends Remote {
public String getGreeting() throws RemoteException;
}
//HelloImpl.java
import java.rmi.*;
import java.rmi.server.*;
public class HelloImpl extends UnicastRemoteObject implements Hello {
public HelloImpl() throws RemoteException {
// No action needed here.
}
public String getGreeting() throws RemoteException {
return ("Hello there!");
}
}
//HelloServer.java
import java.rmi.*;
public class HelloServer {
private static final String HOST = "localhost";
public static void main(String[] args) throws Exception {
// Create a reference to an implementation object...
HelloImpl temp = new HelloImpl();
// Create the string URL holding the object's name...
String rmiObjectName = "rmi://" + HOST + "/Hello";
// (Could omit host name here, since 'localhost‘ would be assumed by default.)
// 'Bind' the object reference to the name...
Naming.rebind(rmiObjectName, temp);
// Display a message so that we know the process has been completed...
System.out.println("Binding complete...\n");
}
}
//HelloClient.java
import java.rmi.*;
public class HelloClient {
private static final String HOST = "localhost";
public static void main(String[] args) {
try {
// Obtain a reference to the object from the registry and typecast it into the
// appropriate
// type...
Hello greeting = (Hello) Naming.lookup("rmi://" + HOST + "/Hello");
// Use the above reference to invoke the remote object's method...
System.out.println("Message received: " + greeting.getGreeting());
} catch (ConnectException conEx) {
System.out.println("Unable to connect to server!");
System.exit(1);
} catch (Exception ex) {
ex.printStackTrace();
System.exit(1);
}
}
}
Problem finally solved here are the steps
Edit the System Environment Variables -> Environment Variables -> User variables
-> CLASSPATH (add if not found) -> path-project-bin-folder
(C:\Users\HP\eclipse\SimpleRMIExample\bin)
for 1st time only
Reboot
for 1st time only
Clean Project
Command: start rmiregistry (on bin folder of your project)
Run project

Error: Could not find or load main class TestClient

I'm attempting to build a HelloWord websocket client with the example code provided here: https://github.com/Gottox/socket.io-java-client/blob/master/examples/basic/BasicExample.java
The goal is to connect to a Flask server that uses: https://github.com/miguelgrinberg/Flask-SocketIO-Chat The client in python I've built works fine, however I also need to build a java client.
I've seen a lot of different solutions that suggest adding a -cp during compiling, but still get the same message Error: Could not find or load main class TestClient What am I doing wrong?
I'm using this script to compile & run.
#!/bin/sh
export JAVA_HOME="/usr/lib/jvm/java-8-oracle"
/usr/lib/jvm/java-8-oracle/bin/javac TestClient.java -cp /home/erm/git/Flask-SocketIO-Chat/*.jar:*:.:./
echo "exitcode:$?"
/usr/lib/jvm/java-8-oracle/bin/java TestClient
Here's the output:
exitcode:0
Error: Could not find or load main class TestClient
TestClient.java
import io.socket.IOAcknowledge;
import io.socket.IOCallback;
import io.socket.SocketIO;
import io.socket.SocketIOException;
import org.json.JSONException;
import org.json.JSONObject;
public class TestClient implements IOCallback {
private SocketIO socket;
/**
* #param args
*/
public static void main(String[] args) {
System.out.println("Hello, World");
try {
new TestClient();
} catch (Exception e) {
e.printStackTrace();
}
}
public TestClient() throws Exception {
socket = new SocketIO();
socket.connect("http://127.0.0.1:5000/", this);
// Sends a string to the server.
socket.send("Hello Server");
// Sends a JSON object to the server.
socket.send(new JSONObject().put("key", "value").put("key2",
"another value"));
// Emits an event to the server.
socket.emit("event", "argument1", "argument2", 13.37);
}
#Override
public void onMessage(JSONObject json, IOAcknowledge ack) {
try {
System.out.println("Server said:" + json.toString(2));
} catch (JSONException e) {
e.printStackTrace();
}
}
#Override
public void onMessage(String data, IOAcknowledge ack) {
System.out.println("Server said: " + data);
}
#Override
public void onError(SocketIOException socketIOException) {
System.out.println("an Error occured");
socketIOException.printStackTrace();
}
#Override
public void onDisconnect() {
System.out.println("Connection terminated.");
}
#Override
public void onConnect() {
System.out.println("Connection established");
}
#Override
public void on(String event, IOAcknowledge ack, Object... args) {
System.out.println("Server triggered event '" + event + "'");
}
}
The error message
Error: Could not find or load main class TestClient
indicates that the Java launcher can't find/load the class file TestClient.class.
To load it properly both the class file itself and the jar file socketio.jar need to be on the class path. So please try
/usr/lib/jvm/java-8-oracle/bin/java -cp .:socketio.jar TestClient
or
/usr/lib/jvm/java-8-oracle/bin/java -cp /home/erm/git/Flask-SocketIO-Chat:/home/erm/git/Flask-SocketIO-Chat/socketio.jar TestClient
The previous answer should work if the file was missing.
But before that, can you please check if the Java file was compiled as it is being run through shell ???
if not please compile the java file
javac TestClient.java
if you are in the path where TestClient.java is present
or provide the full path eg. javac /app/javafiles/TestClient.java

can't run rmi registry on my system

I am learning RMI concepts and had built a simple program taking reference from head first java. All Went fine the first time i ran the code through command prompt.
the next time I ran code the command:
rmiregistry
took too long to load and nothing happened.
I even tried the solution in this thread but nothing happend.
need help to run RMI Registry
also when i run my server and client file i get this error:
Exception: java.rmi.ConnectException: Connection refused to host: 192.168.1.105; nested exception is:
java.net.ConnectException: Connection refused: connect
My Source Code:
myremote.java
import java.rmi.*;
public interface myremote extends Remote
{
public String sayhello() throws RemoteException;
}
Server.java
import java.rmi.*;
import java.rmi.server.*;
public class Server extends UnicastRemoteObject implements myremote
{
public Server() throws RemoteException{}
public String sayhello()
{
return("Server says hi");
}
public static void main(String args[])
{
try
{
myremote S = new Server();
Naming.rebind("remotehello",S);
}
catch(Exception E)
{
System.out.println("Exception: "+E);
}
}
}
client.java
import java.rmi.*;
public class client
{
public static void main(String[] args)
{
client c = new client();
c.go();
}
public void go()
{
try
{
myremote S=(myremote) Naming.lookup("rmi://127.0.0.1/remotehello");
System.out.println("Output:"+S.sayhello());
}
catch(Exception E)
{
System.out.println("Exception: "+ E);
}
}
}
Can't run RMI registry on my system
You've provided no evidence of that.
took too long to load
I don't know what this means. Evidence?
nothing happened.
Nothing is supposed to happen. The RMI registry doesn't print anything. It just sits there.
Run it and try again. You'll be surprised.

JAVA rmi nested exception caused by class not found

I was learning rmi and I came up with a problem and I cannot solve it.
Here is the simple code
client code:
package rmi_test;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
public class client {
public static void main(String [] args) throws RemoteException, NotBoundException
{
Registry registry = LocateRegistry.getRegistry("127.0.0.1");
DNInterface s = (DNInterface) registry.lookup("DNInterface");
if (s.test())
System.out.println("Hello World");
}
}
Interface code:
package rmi_test;
import java.rmi.Remote;
import java.rmi.RemoteException;
public interface DNInterface extends Remote{
public boolean test() throws RemoteException;
}
Server Code:
package rmi_test;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.UnicastRemoteObject;
public class DataNodeImp implements DNInterface{
#Override
public boolean test() throws RemoteException {
System.out.println("test success");
return true;
}
public DataNodeImp()
{
super();
}
private static void boot() throws RemoteException
{
DNInterface d = new DataNodeImp();
DNInterface stub =
(DNInterface) UnicastRemoteObject.exportObject(d, 0);
Registry registry = LocateRegistry.getRegistry();
registry.rebind("DNInterface", stub);
}
public static void main(String[] args) throws RemoteException
{
String name = "DNInterface";
boot();
}
}
Then I use the following commands to compile the code
javac -cp src: src/rmi_test/DNInterface.java
javac -cp src: src/rmi_test/DataNodeImp.java
javac -cp src: src/rmi_test/client.java
then i type
rmiregistry &
When I try to use
java -cp src: rmi_test.DataNodeImp to run the server
it said
Exception in thread "main" java.rmi.ServerException: RemoteException occurred in server thread; nested exception is:
java.rmi.UnmarshalException: error unmarshalling arguments; nested exception is:
java.lang.ClassNotFoundException: rmi_test.DNInterface
Is there anything wrong with the command I run the code?
Why I cannot find DNInterface.class?
Thank you very much!
The Registry doesn't have that class in its CLASSPATH.
The simplest solution is to use LocateRegistry.createRegistry() in the server JVM rather than rmiregistry.

RMI(java.net.ConnectException: Connection refused: connect)

I am trying to run my server named SampleServer. I am using windows and this is what i did:
in cmd:
javaw rmiregistry 1099
cd C:\Users\Home\workspace\RMI\src
java -Djava.security.policy=policy SampleServer 1099
i get the following error:
binding //localhost:1099/Sample
New instance of Sample created
Sample server failed:Connection refused to host: localhost; nested exception is:
java.net.ConnectException: Connection refused: connect
I've tried using a different port # such as 4719 for rmiregistry but i receive the same error. I made sure that my firewall was disabled but the problem persist. I made sure that the port is not already being used. I really hope someone can help me.
Picture of my desktop with folders of project, eclipse window and cmd open:
http://s22.postimg.org/uq00qzslr/picyture.png
Code:
SampleServer:
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.RMISecurityManager;
import java.rmi.server.UnicastRemoteObject;
public class SampleServer {
public static void main(String args[]) {
if (args.length != 1) {
System.err.println("usage: java SampleServer rmi_port");
System.exit(1);
}
// Create and install a security manager
if (System.getSecurityManager() == null)
System.setSecurityManager(new RMISecurityManager());
try {
// first command-line argument is the port of the rmiregistry
int port = Integer.parseInt(args[0]);
String url = "//localhost:" + port + "/Sample";
System.out.println("binding " + url);
Naming.rebind(url, new Sample());
// Naming.rebind("Sample", new Sample());
System.out.println("server " + url + " is running...");
}
catch (Exception e) {
System.out.println("Sample server failed:" + e.getMessage());
}
}
}
SampleClient:
import java.rmi.Naming;
import java.rmi.RemoteException;
public class SampleClient {
public static void main(String args[]) {
try {
if (args.length < 3) {
System.err.println("usage: java SampleClient host port string... \n");
System.exit(1);
}
int port = Integer.parseInt(args[1]);
String url = "//" + args[0] + ":" + port + "/Sample";
System.out.println("looking up " + url);
SampleInterface sample = (SampleInterface)Naming.lookup(url);
// args[2] onward are the strings we want to reverse
for (int i=2; i < args.length; ++i)
// call the remote method and print the return
System.out.println(sample.invert(args[i]));
} catch(Exception e) {
System.out.println("SampleClient exception: " + e);
}
}
}
SampleInterface:
import java.rmi.Remote;
import java.rmi.RemoteException;
public interface SampleInterface extends Remote {
public String invert(String msg) throws RemoteException;
}
Sample:
import java.rmi.Remote;
import java.rmi.RemoteException;
import java.rmi.server.*;
// this is the class with remote methods
public class Sample
extends UnicastRemoteObject
implements SampleInterface {
private int a;
public Sample() throws RemoteException {
System.out.println("New instance of Sample created");
a = 1;
}
public String invert(String m) throws RemoteException {
// return input message with characters reversed
System.out.println("invert("+m+") a=" + a);
return new StringBuffer(m).reverse().toString();
}
}
policy:
grant {
permission java.security.AllPermission;
};
javaw rmiregistry 1099
Stop right there. This is already wrong. 'rmiregistry' is an executable, not the name of a Java class you can execute with 'java' or 'javaw'. Just use 'rmiregistry'.
This error occurs when there is no service running on the port, you are trying to connect. As said by EJP, rmiregistry is a tool which can be started by rmiregistry & in the background (JDK 7). I would recommend you that you check your firewall or connectivity issue with the port.

Categories

Resources