I am working on a RMI client, but when I try to connect to it from any machine but localhost it gets a refused connection stack trace. I verified that my firewall is off. What else do I need to do?
Here is my code
public class FrontManagerClient {
boolean state;
String command;
String value;
public FrontManagerClient(String cmd,String value) {
this.command=cmd;
this.value=value;
}
public boolean getCommand() throws RemoteException, NotBoundException, MalformedURLException{
FrontPanelManagerIntrface f=(FrontPanelManagerIntrface)Naming.lookup("lookup=//localhost//frontPanelRequestControllerService");
state=f.Communicate(command, value);
return state;
}
public static void main(String[] args) throws RemoteException, NotBoundException, MalformedURLException {
FrontManagerClient f = new FrontManagerClient("Password_Enter", "admin");
System.out.println(""+f.getCommand());
}
}
when i run the server and run the client ,it displays this error message
Exception in thread "main" java.rmi.ConnectException: Connection refused to host: 10.96.28.6; nested exception is:
java.net.ConnectException: Connection refused: connect
at sun.rmi.transport.tcp.TCPEndpoint.newSocket(TCPEndpoint.java:619)
at sun.rmi.transport.tcp.TCPChannel.createConnection(TCPChannel.java:216)
at sun.rmi.transport.tcp.TCPChannel.newConnection(TCPChannel.java:202)
at sun.rmi.server.UnicastRef.newCall(UnicastRef.java:340)
at sun.rmi.registry.RegistryImpl_Stub.lookup(Unknown Source)
at java.rmi.Naming.lookup(Naming.java:101)
at doormanager.FrontManagerClient.getCommand(FrontManagerClient.java:30)
at doormanager.FrontManagerClient.main(FrontManagerClient.java:37)
Caused by: java.net.ConnectException: Connection refused: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:79)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:339)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:579)
at java.net.Socket.connect(Socket.java:528)
at java.net.Socket.<init>(Socket.java:425)
at java.net.Socket.<init>(Socket.java:208)
at sun.rmi.transport.proxy.RMIDirectSocketFactory.createSocket(RMIDirectSocketFactory.java:40)
at sun.rmi.transport.proxy.RMIMasterSocketFactory.createSocket(RMIMasterSocketFactory.java:146)
at sun.rmi.transport.tcp.TCPEndpoint.newSocket(TCPEndpoint.java:613)
... 7 more
Java Result: 1
There is no RMI Registry running at the host 10.96.28.6, or you haven't fixed your firewall.
Related
I have to embed power BI report using service principle in my java application. so i am trying to generate AAD access token. But I fail to generate access token. Below is the code and output. I have tried to generate aad access token using Javascript also but could not do so. Please help.
Find my code here
import java.net.MalformedURLException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import com.microsoft.aad.adal4j.AuthenticationCallback;
import com.microsoft.aad.adal4j.AuthenticationContext;
import com.microsoft.aad.adal4j.AuthenticationResult;
import com.microsoft.aad.adal4j.ClientCredential;
public class GenerateToken {
public static void main(String[] args) throws InterruptedException, ExecutionException {
System.out.println("Connecting to Azure......");
System.out.println("AAD Token" + ServicePrinciple.getAADAccessTokenFromAzure().get().getAccessToken());
}
}
final class ServicePrinciple {
static String authority = "https://login.microsoftonline.com/tenant_id/oauth2/token";
static String clientId = "";
static String clientSecret = "";
static String resource = "https://analysis.windows.net/powerbi/api";
private static Future<AuthenticationResult> getAADAccessToken(String authority, String resource, String clientId, String clientSecret) {
AuthenticationCallback callback = null;
ClientCredential clientCredential = null;
ExecutorService executorService = Executors.newFixedThreadPool(1);
clientCredential = new ClientCredential(clientId, clientSecret);
AuthenticationContext context = null;
try {
context = new AuthenticationContext(authority, false, executorService);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Future<AuthenticationResult> authenticationResult = context.acquireToken(resource, clientCredential, callback);
return authenticationResult;
}
static Future<AuthenticationResult> getAADAccessTokenFromAzure() {
return getAADAccessToken(authority, resource, clientId, clientSecret);
}
}
Output of my code
Connecting to Azure......
[pool-1-thread-1] INFO com.microsoft.aad.adal4j.AuthenticationAuthority - [Correlation ID: 9f8f9bde-fc94-4cb9-bf91-1a3ef2e79d10] Instance discovery was successful
[pool-1-thread-1] ERROR com.microsoft.aad.adal4j.AuthenticationContext - [Correlation ID: 9f8f9bde-fc94-4cb9-bf91-1a3ef2e79d10] Request to acquire token failed.
java.net.UnknownHostException: login.microsoftonline.com
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:184)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:589)
at sun.security.ssl.SSLSocketImpl.connect(SSLSocketImpl.java:673)
at sun.security.ssl.BaseSSLSocketImpl.connect(BaseSSLSocketImpl.java:173)
at sun.net.NetworkClient.doConnect(NetworkClient.java:180)
at sun.net.www.http.HttpClient.openServer(HttpClient.java:463)
at sun.net.www.http.HttpClient.openServer(HttpClient.java:558)
at sun.net.www.protocol.https.HttpsClient.<init>(HttpsClient.java:264)
at sun.net.www.protocol.https.HttpsClient.New(HttpsClient.java:367)
at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.getNewHttpClient(AbstractDelegateHttpsURLConnection.java:191)
at sun.net.www.protocol.http.HttpURLConnection.plainConnect0(HttpURLConnection.java:1156)
at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:1050)
at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(AbstractDelegateHttpsURLConnection.java:177)
at sun.net.www.protocol.http.HttpURLConnection.getOutputStream0(HttpURLConnection.java:1334)
at sun.net.www.protocol.http.HttpURLConnection.getOutputStream(HttpURLConnection.java:1309)
at sun.net.www.protocol.https.HttpsURLConnectionImpl.getOutputStream(HttpsURLConnectionImpl.java:259)
at com.microsoft.aad.adal4j.AdalOAuthRequest.configureHeaderAndExecuteOAuthCall(AdalOAuthRequest.java:117)
at com.microsoft.aad.adal4j.AdalOAuthRequest.send(AdalOAuthRequest.java:72)
at com.microsoft.aad.adal4j.AdalTokenRequest.executeOAuthRequestAndProcessResponse(AdalTokenRequest.java:72)
at com.microsoft.aad.adal4j.AuthenticationContext.acquireTokenCommon(AuthenticationContext.java:726)
at com.microsoft.aad.adal4j.AuthenticationContext.access$100(AuthenticationContext.java:61)
at com.microsoft.aad.adal4j.AuthenticationContext$1.call(AuthenticationContext.java:128)
at com.microsoft.aad.adal4j.AuthenticationContext$1.call(AuthenticationContext.java:117)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)
Exception in thread "main" java.util.concurrent.ExecutionException: java.net.UnknownHostException: login.microsoftonline.com
at java.util.concurrent.FutureTask.report(FutureTask.java:122)
at java.util.concurrent.FutureTask.get(FutureTask.java:192)
at com.inetpsa.spd.raloyalty.action.GenerateToken.main(GenerateToken.java:25)
Caused by: java.net.UnknownHostException: login.microsoftonline.com
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:184)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:589)
at sun.security.ssl.SSLSocketImpl.connect(SSLSocketImpl.java:673)
at sun.security.ssl.BaseSSLSocketImpl.connect(BaseSSLSocketImpl.java:173)
at sun.net.NetworkClient.doConnect(NetworkClient.java:180)
at sun.net.www.http.HttpClient.openServer(HttpClient.java:463)
at sun.net.www.http.HttpClient.openServer(HttpClient.java:558)
at sun.net.www.protocol.https.HttpsClient.<init>(HttpsClient.java:264)
at sun.net.www.protocol.https.HttpsClient.New(HttpsClient.java:367)
at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.getNewHttpClient(AbstractDelegateHttpsURLConnection.java:191)
at sun.net.www.protocol.http.HttpURLConnection.plainConnect0(HttpURLConnection.java:1156)
at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:1050)
at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(AbstractDelegateHttpsURLConnection.java:177)
at sun.net.www.protocol.http.HttpURLConnection.getOutputStream0(HttpURLConnection.java:1334)
at sun.net.www.protocol.http.HttpURLConnection.getOutputStream(HttpURLConnection.java:1309)
at sun.net.www.protocol.https.HttpsURLConnectionImpl.getOutputStream(HttpsURLConnectionImpl.java:259)
at com.microsoft.aad.adal4j.AdalOAuthRequest.configureHeaderAndExecuteOAuthCall(AdalOAuthRequest.java:117)
at com.microsoft.aad.adal4j.AdalOAuthRequest.send(AdalOAuthRequest.java:72)
at com.microsoft.aad.adal4j.AdalTokenRequest.executeOAuthRequestAndProcessResponse(AdalTokenRequest.java:72)
at com.microsoft.aad.adal4j.AuthenticationContext.acquireTokenCommon(AuthenticationContext.java:726)
at com.microsoft.aad.adal4j.AuthenticationContext.access$100(AuthenticationContext.java:61)
at com.microsoft.aad.adal4j.AuthenticationContext$1.call(AuthenticationContext.java:128)
at com.microsoft.aad.adal4j.AuthenticationContext$1.call(AuthenticationContext.java:117)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)
I have almost the same code working (for the last day or so)
However my code snippet look like the folowing
String authority = "https://login.microsoftonline.com/{your tenant id}";
ExecutorService service = Executors.newFixedThreadPool(1);
AuthenticationContext context = new AuthenticationContext(authority, true, service);
Are you behind a firewall that isn't allowing you reach login.microsoftonline.com?
You can use the following API page to login and generate a report token
https://learn.microsoft.com/en-gb/rest/api/power-bi/embedtoken/reports_generatetokeningroup
is your login.microsoftonline.com behind a proxy? if so, set your proxy with the either set of jvm arguments depending if it is http or https:
HTTPS
-Dhttps.proxyHost=proxy-name-without-https.com -Dhttps.proxyPort=proxy-port
ex. -Dhttps.proxyHost=myproxy.com -Dhttps.proxyPort=2021
or
HTTP
-Dhttp.proxyHost=proxy-name-without-http.com -Dhttp.proxyPort=proxy-port
ex. -Dhttp.proxyHost=myproxy.com -Dhttp.proxyPort=2021
Help me! I have a restful API link, I used Postman tool call success, but when I code on eclipse a Restful client, it's fail. I tried on off proxy, but not success.
public class JersyGetClient {
public static void main(String[] args) {
String url = "https://abc.custhelp.com/services/rest/connect/v1.4/accounts";
String name = "thai.hoangan";
String password = "123456a#";
String authString = name + ":" + password;
String authStringEnc = new BASE64Encoder().encode(authString.getBytes());
System.out.println("Base64 encoded auth string: " + authStringEnc);
Client restClient = Client.create();
WebResource webResource = restClient.resource(url);
ClientResponse resp = webResource.accept("application/json")
.header("Authorization", "Basic " + authStringEnc)
.header("OSvC-CREST-Application-Context", "asd")
.get(ClientResponse.class);
if (resp.getStatus() != 200) {
System.err.println("Unable to connect to the server");
}
String output = resp.getEntity(String.class);
System.out.println("response: " + output);
}
}
Output:
Exception in thread "main" com.sun.jersey.api.client.ClientHandlerException:
java.net.ConnectException: Connection timed out: connect
at com.sun.jersey.client.urlconnection.URLConnectionClientHandler.handle(URLConnectionClientHandler.java:155)
at com.sun.jersey.api.client.Client.handle(Client.java:652)
at com.sun.jersey.api.client.WebResource.handle(WebResource.java:682)
at com.sun.jersey.api.client.WebResource.access$200(WebResource.java:74)
at com.sun.jersey.api.client.WebResource$Builder.get(WebResource.java:509)
at JersyGetClient.main(JersyGetClient.java:22)
Caused by: java.net.ConnectException: Connection timed out: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:79)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:589)
at sun.security.ssl.SSLSocketImpl.connect(SSLSocketImpl.java:668)
at sun.security.ssl.BaseSSLSocketImpl.connect(BaseSSLSocketImpl.java:173)
at sun.net.NetworkClient.doConnect(NetworkClient.java:180)
at sun.net.www.http.HttpClient.openServer(HttpClient.java:432)
at sun.net.www.http.HttpClient.openServer(HttpClient.java:527)
at sun.net.www.protocol.https.HttpsClient.(HttpsClient.java:264)
at sun.net.www.protocol.https.HttpsClient.New(HttpsClient.java:367)
at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.getNewHttpClient(AbstractDelegateHttpsURLConnection.java:191)
at sun.net.www.protocol.http.HttpURLConnection.plainConnect0(HttpURLConnection.java:1105)
at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:999)
at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(AbstractDelegateHttpsURLConnection.java:177)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1513)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1441)
at java.net.HttpURLConnection.getResponseCode(HttpURLConnection.java:480)
at sun.net.www.protocol.https.HttpsURLConnectionImpl.getResponseCode(HttpsURLConnectionImpl.java:338)
at com.sun.jersey.client.urlconnection.URLConnectionClientHandler._invoke(URLConnectionClientHandler.java:253)
at com.sun.jersey.client.urlconnection.URLConnectionClientHandler.handle(URLConnectionClientHandler.java:153)
... 5 more
Oh, I resolved, Just add:
System.setProperty("https.proxyHost", "proxytest.vcb");
System.setProperty("https.proxyPort", "8080");
This is my code
public class Email {
static Properties props;
public static void main(String[] args){
String from = "some email";
String to= "some other email";
String host = "localhost";
props = System.getProperties();
props.setProperty("mail.host", host);
props.put("mail.transport.protocol", "smtp");
Session session = Session.getDefaultInstance(props,null);
try{
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
message.setSubject("This is the Subject Line!");
message.setText("This is actual message");
Transport.send(message);
System.out.println("Sent message successfully....");
}catch (MessagingException mex) {
mex.printStackTrace();
}
}
}
I am getting following exception:
javax.mail.MessagingException: Could not connect to SMTP host: localhost, port: 25;
nested exception is:
java.net.ConnectException: Connection refused
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1961)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:654)
at javax.mail.Service.connect(Service.java:295)
at javax.mail.Service.connect(Service.java:176)
at javax.mail.Service.connect(Service.java:125)
at javax.mail.Transport.send0(Transport.java:194)
at javax.mail.Transport.send(Transport.java:124)
at com.cisco.ci.support.email.Email.main(Email.java:51)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)
Caused by: java.net.ConnectException: Connection refused
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:589)
at java.net.Socket.connect(Socket.java:538)
at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:321)
at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:237)
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1927)
... 12 more
It seems that I do not have smtp server on my mac? any solutions?
You either need to install a smtp Server on your mac, which is not that simple, or use an existing smtp server. Obviously you will need an account for that server, as there are not many smtp Servers without authentication in the wild.
E.g. Create a googlemail Account, then check: http://www.mkyong.com/java/javamail-api-sending-email-via-gmail-smtp-example/ for example of how to use it!
Here is the error.
com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:408)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1117)
at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:355)
at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2461)
at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2498)
at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2283)
at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:822)
at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:47)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:408)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:404)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:317)
at java.sql.DriverManager.getConnection(DriverManager.java:664)
at java.sql.DriverManager.getConnection(DriverManager.java:247)
at eventmanagementsystem.infinity.dbHandler.buildConnection(dbHandler.java:43)
at eventmanagementsystem.infinity.dbHandler.getParticipantResultSet(dbHandler.java:395)
at eventmanagementsystem.infinity.ParticipantList.populateList(ParticipantList.java:96)
at eventmanagementsystem.infinity.EMS.<init>(EMS.java:31)
at eventmanagementsystem.infinity.EventManagementSystemInfinity.main(EventManagementSystemInfinity.java:25)
Caused by: java.net.ConnectException: Connection refused: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:79)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:345)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:589)
at java.net.Socket.connect(Socket.java:538)
at java.net.Socket.<init>(Socket.java:434)
at java.net.Socket.<init>(Socket.java:244)
at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:259)
at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:305)
... 19 more
May 24, 2015 3:48:58 PM eventmanagementsystem.infinity.EMS <init>
SEVERE: null
java.lang.NullPointerException
at eventmanagementsystem.infinity.dbHandler.getParticipantResultSet(dbHandler.java:396)
at eventmanagementsystem.infinity.ParticipantList.populateList(ParticipantList.java:96)
at eventmanagementsystem.infinity.EMS.<init>(EMS.java:31)
at eventmanagementsystem.infinity.EventManagementSystemInfinity.main(EventManagementSystemInfinity.java:25)
BUILD SUCCESSFUL (total time: 21 seconds)
Here is the code.
public static Connection buildConnection()
{
try{
//Class.forName("org.apache.derby.jdbc.ClientDriver");
//Class.forName("oracle.jdbc.driver.OracleDriver");
Class.forName("com.mysql.jdbc.Driver");
//String connString = "jdbc:derby://localhost:4040/EventManagementSystem";
//String username = "Event";
//String password = "event";
//String username = "system";
//String password = "a1234";
String JDBC_DRIVER = "com.mysql.jdbc.Driver";
String DB_URL = "jdbc:mysql://localhost:3306/user";
String USER = "root";
String PASS = "a1234";
//Connection conn = DriverManager.getConnection(connString,username,password);
//Connection conn = DriverManager.getConnection("jdbc:oracle:thin:#oracle-prod:1521:OPROD",username,password);
Connection conn = DriverManager.getConnection(DB_URL,USER,PASS);
return conn;
}
catch(Exception e)
{
e.printStackTrace();
}
return null;
}
I think the problem is with the DB_URL but I am not very sure. I have added mysql-connector jar as well. It cannot access the database that I have created. The error list also shows that connection is refused but I am not sure why is that so.
I have this strange problem.
I'm trying to write a simple Hello World java program using RMI.
Here are my files, all belonging to the same package:
Hello.java:
package com.something;
import java.rmi.Remote;
import java.rmi.RemoteException;
public interface Hello extends Remote{
String hello() throws RemoteException;
}
Server.java
package com.something;
import com.something.Hello;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.UnicastRemoteObject;
public class Server implements Hello{
#Override
public String hello() throws RemoteException {
return "hello world!!!!";
}
public static void main (String args[]) throws RemoteException{
//System.setProperty("java.rmi.server.hostname", "10.0.0.5");
if (System.getSecurityManager() == null) {
System.setSecurityManager(new SecurityManager());
}
try {
String name = "Hello";
Hello s = new Server();
Hello stub =
(Hello) UnicastRemoteObject.exportObject(s, 0);
Registry registry = LocateRegistry.getRegistry(8888);
registry.rebind(name, stub);
System.out.println("Hello bound");
} catch (Exception e) {
System.err.println("Hello exception:");
e.printStackTrace();
}
}
}
Client.java:
package com.something;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
public class Client {
public static void main(String args[]) {
if (System.getSecurityManager() == null) {
System.setSecurityManager(new SecurityManager());
}
try {
String name = "Hello";
Registry registry = LocateRegistry.getRegistry("localhost");
Hello h = (Hello) registry.lookup(name);
System.out.println(h.hello());
} catch (Exception e) {
System.err.println("My exception:");
e.printStackTrace();
}
}
}
Before runnig the code, in the terminal, I run:
rmiregistry 8888
I run the java code with the following flag:
-Djava.security.policy=/Users/nataliazon/Documents/workspace/SiecioweRMI/src/server.policy
And here's my server.policy file:
grant {
permission java.security.AllPermission;
};
When I run the Server program like that, the following exception is thrown:
Hello exception:
java.rmi.ServerException: RemoteException occurred in server thread; nested exception is:
java.rmi.UnmarshalException: error unmarshalling arguments; nested exception is:
java.lang.ClassNotFoundException: com.something.Hello
at sun.rmi.server.UnicastServerRef.oldDispatch(UnicastServerRef.java:400)
at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:248)
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)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
at java.lang.Thread.run(Thread.java:680)
at sun.rmi.transport.StreamRemoteCall.exceptionReceivedFromServer(StreamRemoteCall.java:255)
at sun.rmi.transport.StreamRemoteCall.executeCall(StreamRemoteCall.java:233)
at sun.rmi.server.UnicastRef.invoke(UnicastRef.java:359)
at sun.rmi.registry.RegistryImpl_Stub.rebind(Unknown Source)
at com.something.Server.main(Server.java:30)
Caused by: java.rmi.UnmarshalException: error unmarshalling arguments; nested exception is:
java.lang.ClassNotFoundException: com.something.Hello
at sun.rmi.registry.RegistryImpl_Skel.dispatch(Unknown Source)
at sun.rmi.server.UnicastServerRef.oldDispatch(UnicastServerRef.java:390)
at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:248)
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)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
at java.lang.Thread.run(Thread.java:680)
Caused by: java.lang.ClassNotFoundException: com.something.Hello
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:247)
at sun.rmi.server.LoaderHandler.loadProxyInterfaces(LoaderHandler.java:709)
at sun.rmi.server.LoaderHandler.loadProxyClass(LoaderHandler.java:653)
at sun.rmi.server.LoaderHandler.loadProxyClass(LoaderHandler.java:590)
at java.rmi.server.RMIClassLoader$2.loadProxyClass(RMIClassLoader.java:628)
at java.rmi.server.RMIClassLoader.loadProxyClass(RMIClassLoader.java:294)
at sun.rmi.server.MarshalInputStream.resolveProxyClass(MarshalInputStream.java:238)
at java.io.ObjectInputStream.readProxyDesc(ObjectInputStream.java:1530)
at java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:1492)
at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1731)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1328)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:350)
... 12 more
And the Client program throws this:
My exception:
java.rmi.ConnectException: Connection refused to host: localhost; nested exception is:
java.net.ConnectException: Connection refused
at sun.rmi.transport.tcp.TCPEndpoint.newSocket(TCPEndpoint.java:601)
at sun.rmi.transport.tcp.TCPChannel.createConnection(TCPChannel.java:198)
at sun.rmi.transport.tcp.TCPChannel.newConnection(TCPChannel.java:184)
at sun.rmi.server.UnicastRef.newCall(UnicastRef.java:322)
at sun.rmi.registry.RegistryImpl_Stub.lookup(Unknown Source)
at com.something.Client.main(Client.java:18)
Caused by: java.net.ConnectException: Connection refused
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:351)
at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:213)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:200)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:432)
at java.net.Socket.connect(Socket.java:529)
at java.net.Socket.connect(Socket.java:478)
at java.net.Socket.<init>(Socket.java:375)
at java.net.Socket.<init>(Socket.java:189)
at sun.rmi.transport.proxy.RMIDirectSocketFactory.createSocket(RMIDirectSocketFactory.java:22)
at sun.rmi.transport.proxy.RMIMasterSocketFactory.createSocket(RMIMasterSocketFactory.java:128)
at sun.rmi.transport.tcp.TCPEndpoint.newSocket(TCPEndpoint.java:595)
... 5 more
How can this problem be solved?
java.lang.ClassNotFoundException: com.something.Hello
The Registry and the client don't have that class on their classpath.
In the case of the Registry the simplest solution is to start it in the server JVM. via LocateRegistry.createRegistry(): or else start it in such a way that the class is on its classpath; there are several ways to accomplish that.
java.rmi.ConnectException: Connection refused to host: localhost
That is caused by specifying "localhost" instead of the server host when calling Naming.lookup() or when calling LocateRegistry.getRegistry() prior to calling Registry.lookup(). The Registry isn't ubiquitous, it is located at the server host.
NB you don't need the SecurityManager unless you are using the RMI codebase feature or you have some other requirement for it.