Java RMI from external IP - java

I used java RMI to connect two java applications. if i run them both locally (and change ip to localhost) it works, if i run them both on my server it works. but if i run Client.java locally and ApplicationServer.java on my server it throws this on the client side:
Exception in thread "main" java.rmi.ConnectException: Connection refused to host: my.servers.private.ip; nested exception is:
java.net.ConnectException: Connection timed out: connect ...
Client:
public class Client {
public static void main(String[] args) throws RemoteException, MalformedURLException NotBoundException {
System.out.println("attempting connection");
System.setProperty("java.security.policy","test.policy");
HelloService service =(HelloService) Naming.lookup("rmi://my.servers.private.ip:1001/hello");
System.out.println("connected");
if(service.isConnected()) {
System.out.println("success.");
}
}
}
Server:
public class ApplicationServer {
public static void main(String[] args) throws RemoteException, AlreadyBoundException {
System.setProperty("java.security.policy","test.policy");
System.setProperty("java.rmi.server.hostname","my.servers.private.ip");
Registry registry = LocateRegistry.createRegistry(1001);
registry.rebind("hello", new HelloServant());
System.out.println("ready");
}
}
If i change the code from my.servers.private.ip to my.servers.public.ip and run both on my server it doesn't work either.
Changing my.servers.private.ip to my.servers.public.ip does not work.
I have a feeling it has to do something with the public and private IP.

Related

InetAddress.getLocalHost() fails intermittently

I am debugging an application which is failing to bootup with the UnknownHostException exception while it's trying to fetch the hostname. So to reproduce this I setup the machine and manually turned off all the DNS resolutions and ran the same application.
Remove the hostname and localhost entries from /etc/hosts.
Remove the host to IP mapping from my DNS server.
I was able to successfully reproduce and it fails with the same exception.
InetAddress local;
try {
local = InetAddress.getLocalHost();
} catch (UnknownHostException e) {
throw new MalformedURLException("Local host name unknown: " +
e);
}
However in the same machine I wrote a simple Java code to do the same resolution but I don't see the issue when I run my script.
Here is what I am running
import java.net.InetAddress;
import java.net.UnknownHostException;
public class TestDNSResolution {
public static void main(String[] args) throws UnknownHostException {
try {
InetAddress inetAddress;
inetAddress = InetAddress.getLocalHost();
System.out.printf("inetAddress: %s", inetAddress);
} catch (UnknownHostException e) {
e.printStackTrace();
}
}
}
Output:
$ java TestDNSResolution
inetAddress: ****<actualhostname>****/10.1.6.109
I was actually expecting this to throw an exception.
Any pointers on what I am missing or anyways I can make this fail from the Java code?

SSHJ is not able to connect remote Linux server throws UserAuthException: Exhausted available authentication methods

I am trying to run the following Java code using sshj :-
public static void main(String[] args) throws IOException {
SSHClient ssh = new SSHClient();
ssh.loadKnownHosts();
ssh.connect("host", port);
try {
ssh.authPassword("user", "passwd");
ssh.useCompression();
final String src = System.getProperty("user.home") + File.separator + "test_file";
ssh.newSCPFileTransfer().upload(new FileSystemFile(src), "/tmp/");
} finally {
ssh.disconnect();
ssh.close();
}
}
But it is throwing exception -
Exception in thread "main" net.schmizz.sshj.userauth.UserAuthException: Exhausted available authentication methods
at net.schmizz.sshj.SSHClient.auth(SSHClient.java:231)
at net.schmizz.sshj.SSHClient.auth(SSHClient.java:206)
at net.schmizz.sshj.SSHClient.authPassword(SSHClient.java:292)
at net.schmizz.sshj.SSHClient.authPassword(SSHClient.java:262)
at net.schmizz.sshj.SSHClient.authPassword(SSHClient.java:246)
at sample.SCPUpload.main(SCPUpload.java:17)
I can connect the host using same credentials via Putty. I am using JDK "1.8.0_151". What is wrong here?
Typically it means that either your password is wrong, or you're not allowed to connect using the 'password' authentication method.

How to make a connection to the mysql database in openshift 3.0

I'm testing openshift 3.0 and I cannot figure out how to make a simple connection in java. I've done port forwarding: local port 3306 for remote port 3306 and I'm using the following code:
public class testDAO {
public static void main(String[] args) throws SQLException {
Connection conexao = DriverManager.getConnection(
"jdbc:mysql://127.0.0.1:3306/sampledb","userFGF","eA3Rwe4OgSiWGeK2");
System.out.println("Conectado!");
conexao.close();
}
}

Java: Duplicated port bind by jetty7 under jdk7

I have met an issue that the jetty7 server didn't throw 'Address already in use' exception when the port was occupied by other application. But it had worked well on jdk6.
Environment:
Server: windows 2008 server
Java: 7.0.20.13
Jetty: 7.6.0.RC4
Below is my demo code:
public class Jetty7OnJdk7 {
public static void main(String[] args) throws Exception {
Server server = new Server();
SelectChannelConnector connector = new SelectChannelConnector(); //this is the default connect in jetty7
connector.setReuseAddress(false); //prevent the jetty reusing the occupied port
connector.setPort(80);
server.setConnectors(new Connector[]{connector});
server.start();
System.out.println("=========:" + server);
}
}

Java RMI ServerException - java.lang.ClassNotFoundException: org.prog.rmi.RmiServer_Stub

I have inherited some Java RMI client/server code, and while it runs fine on one machine, I haven't been able to get it to run in my dev environment.
The problem is when I run the server using the following java.exe -Djava.security.policy=conf\server.policy -SRC;. -Djava.library.path=. org.prog.rmi.RmiServer
I get the following error:
java.rmi.ServerException: RemoteException occurred in server thread; nested exception is:
java.rmi.UnmarshalException: error unmarshalling arguments; nested exception is:
java.lang.ClassNotFoundException: org.prog.rmi.RmiServer_Stub (no security manager: RMI class loader disabled)
at sun.rmi.server.UnicastServerRef.oldDispatch(UnicastServerRef.java:396)
at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:250)
at sun.rmi.transport.Transport$1.run(Transport.java:159)
at java.security.AccessController.doPrivileged(Native Method)
at sun.rmi.transport.Transport.serviceCall(Transport.java:155)
at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:535)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:790)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:649)
...
My server.policy file is
grant {
permission java.security.AllPermission;
};
And my java code:
package org.prog.rmi;
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.RMISecurityManager;
import java.rmi.server.UnicastRemoteObject;
import java.rmi.registry.*;
public class RmiServer extends UnicastRemoteObject
implements RmiServerIntf {
private BatchApi bapi;
private String iniFileLocation;
private String layoutOption;
private int addressCount = 0;
private RefInt apiHandle = new RefInt();
public RmiServer(String iniFileLocation,String layoutOption) throws RemoteException
{ super();
this.iniFileLocation = iniFileLocation;
this.layoutOption = layoutOption;
initAPI();
startupAPI();
openAPI();
}
public static void main(String args[])
{
System.out.println("RMI server started");
// Create and install a security manager
if (System.getSecurityManager() == null)
{
System.setSecurityManager(new RMISecurityManager());
System.out.println("Security manager installed.");
}
else
System.out.println("Security manager already exists.");
try //special exception handler for registry creation
{
LocateRegistry.createRegistry(1099);
System.out.println("java RMI registry created.");
}
catch (RemoteException e)
{
//do nothing, error means registry already exists
System.out.println("java RMI registry already exists.");
}
try
{
//Instantiate RmiServer
for (String arg: args){
System.out.println(arg);
}
RmiServer obj = new RmiServer(args[0],args[1]);
// Bind this object instance to the name "RmiServer"
Naming.rebind("//127.0.0.1/RmiServer", obj);
System.out.println("PeerServer bound in registry");
}
catch (Exception e)
{
System.err.println("RMI server exception:");
e.printStackTrace();
}
}
}
I've seen solutions relating to the java.rmi.server.codebase but didn't have any luck setting this either
You haven't regenerated the stub with rmic, or the Registry doesn't have access to it via its classpath.
After some further investigation and following RMI tutorials it appeared that there was a problem with the RMI registration server on port 1099.
When I stared the RMI registration server on another port (e.g. 2005) and changed these lines of code
LocateRegistry.createRegistry(2005);
and
Naming.rebind("//127.0.0.1:2055/RmiServer", obj);
This ran sucessfully without errors and my client was able to connect.
I hope this answer helps others with this error. Let me know if anyone needs any more detailed information.

Categories

Resources