Run Java Client on VMware Virtual Machine - java

I have a client-server Java project, from which I have generated a JAR file for the client execution.
When the JAR file is not running on Vmware, it easily connects to the server (which is running in IntelliJ).
However, it cannot connect to the server when it is on the virtual machine.
It gives me this error:
java.net.ConnectException: Connection refused (Connection refused)
at java.base/java.net.PlainSocketImpl.socketConnect(Native Method)
Has anyone had any similar experience?

Related

Unable to connect Docker Container using HttpURLConnection in Java

I have created two Web Projects in java.
One of the project was running inside the Tomcat docker container:
192.160.1.5:8080/SocketServer
Another project was running in local machine using eclipse
localhost:8081/SocketClient
I am trying to connect the SocketServer project running inside docker container from the SocketClient using HttpURLConnection in java. But it gives some errors like below:
java.net.ConnectException: Connection refused
at java.base/sun.nio.ch.Net.connect0(Native Method)
at java.base/sun.nio.ch.Net.connect(Net.java:574)
at java.base/sun.nio.ch.Net.connect(Net.java:563)
at java.base/sun.nio.ch.NioSocketImpl.connect(NioSocketImpl.java:588)
at java.base/java.net.SocksSocketImpl.connect(SocksSocketImpl.java:333)
at java.base/java.net.Socket.connect(Socket.java:648)
at java.base/java.net.Socket.connect(Socket.java:597)
at java.base/java.net.Socket.<init>(Socket.java:520)
at java.base/java.net.Socket.<init>(Socket.java:370)
at org.apache.commons.httpclient.protocol.DefaultProtocolSocketFactory.createSocket(DefaultProtocolSocketFactory.java:80)
at org.apache.commons.httpclient.protocol.DefaultProtocolSocketFactory.createSocket(DefaultProtocolSocketFactory.java:122)
at org.apache.commons.httpclient.HttpConnection.open(HttpConnection.java:707)
at org.apache.commons.httpclient.HttpMethodDirector.executeWithRetry(HttpMethodDirector.java:387)
at org.apache.commons.httpclient.HttpMethodDirector.executeMethod(HttpMethodDirector.java:171)
at org.apache.commons.httpclient.HttpClient.executeMethod(HttpClient.java:397)
at org.apache.commons.httpclient.HttpClient.executeMethod(HttpClient.java:323)
at com.dump.LogAction.startSocket(LogAction.java:61)
at com.dump.LogAction.execute(LogAction.java:37)
How can I resolve this?
This is normal behavior, you should read the docs.
https://docs.docker.com/network/network-tutorial-host/
Quick answer: run the docker image with --network host argument OR you need to expose a port publicly https://www.whitesourcesoftware.com/free-developer-tools/blog/docker-expose-port/ --expose 8080

Jenkins CLI connection refused

When executing
java -jar jenkins-cli.jar -s https://jenkins_url help
I'm getting connection refused for some reason. Jenkins version is 1580.3, user has the permissions on Jenkins server, cli jar is up-to-date and ssh public key is configured properly. Any idea what might be causing this? Here's the stack trace if it will help:
Exception in thread "main" java.net.ConnectException: Connection refused
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at hudson.cli.CLI.connectViaCliPort(CLI.java:203)
at hudson.cli.CLI.<init>(CLI.java:126)
at hudson.cli.CLIConnectionFactory.connect(CLIConnectionFactory.java:72)
at hudson.cli.CLI._main(CLI.java:466)
at hudson.cli.CLI.main(CLI.java:382)
Suppressed: java.io.EOFException: unexpected stream termination
at hudson.remoting.ChannelBuilder.negotiate(ChannelBuilder.java:331)
at hudson.remoting.Channel.<init>(Channel.java:421)
at hudson.remoting.Channel.<init>(Channel.java:400)
at hudson.remoting.Channel.<init>(Channel.java:396)
at hudson.remoting.Channel.<init>(Channel.java:385)
at hudson.remoting.Channel.<init>(Channel.java:377)
at hudson.remoting.Channel.<init>(Channel.java:353)
at hudson.cli.CLI.connectViaHttp(CLI.java:157)
at hudson.cli.CLI.<init>(CLI.java:130)
... 3 more
If the connection is refused, perhaps the CLI is disabled, but more likely there's a firewall or network issue.
Each successful HTTP request to Jenkins responds with the CLI port as an HTTP header, so you can quickly check whether it's enabled:
curl -sI http://jenkins/ | grep CLI
Alternatively, as documented on the wiki, you can check whether the TCP port is enabled. Look for "TCP port for JNLP slave agents" at http://jenkins/configureSecurity/
However, since your stacktrace indicates that the CLI client is trying to establish a connection to a particular port, it suggests that the CLI is enabled, and Jenkins is advertising that port number in its HTTP responses.
So you could try to connect to that port manually, to check whether it's open:
telnet jenkins <cli-port>
If you also see "Connection refused" here, then you know that your Jenkins and CLI client setup seem to be ok, but you have a network issue. In which case, check that the firewall on the Jenkins server allows incoming connections on that CLI port.
To make firewall configuration easier, you can switch from a random port to a fixed port in the "configure security" page mentioned above.
I had this issue on Ubuntu 16.04.
Note: this error happened to me well after setting up and having ran successful build jobs before. This happened to me after running a particularly heavy build job. So my fix might work assuming that the error isn't a firewall issue.
First, I tried to do a safe restart through the commandline using the jenkins-cli.jar tool:
java -jar jenkins-cli.jar -s http://localhost:8080 -auth <username>:<password> safe-restart
but also got the same Connection refused error message.
So as a last resort I tried restarting the Jenkins service on the server like so:
sudo systemctl restart jenkins
and that fixed it.

trouble connecting to oracle db through ojdbc

For those who are willing to read a bit, (skip to end for short version):
I am trying to connect my java app to a remote oracle database. with no luck I am using Mac 10.7.5 with IntelliJ Idea 12, java version "1.6.0_51" 64-bit. I have been trying to get this work all day with no success. I also was not able to use the 'DataSource' view in IntelliJ because of a missing shared dylib missing in java.library.path. I suspect the two issues are related.
I tried installing Oracle Instant Client 64-bit (including the jdbc files), copied the dylibs to /usr/lib/java and this got me close to solving the "DataSource" view issue with a different error:
java.lang.RuntimeException: java.rmi.UnmarshalException: Error unmarshaling return header; nested exception is:
java.io.EOFException
in RemoteUtil$RemoteInvocationHandler.invoke(RemoteUtil.java:219)
at com.sun.proxy.$Proxy115.connect(Unknown Source)
in LocalDataSource.getConnection(LocalDataSource.java:158)
Caused by: java.rmi.UnmarshalException: Error unmarshaling return header; nested exception is:
java.io.EOFException
at sun.rmi.transport.StreamRemoteCall.executeCall(StreamRemoteCall.java:209)
at sun.rmi.server.UnicastRef.invoke(UnicastRef.java:142)
at java.rmi.server.RemoteObjectInvocationHandler.invokeRemoteMethod(RemoteObjectInvocationHandler.java:178)
at java.rmi.server.RemoteObjectInvocationHandler.invoke(RemoteObjectInvocationHandler.java:132)
at com.sun.proxy.$Proxy114.connect(Unknown Source)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
in RemoteUtil$RemoteInvocationHandler.invoke(RemoteUtil.java:211)
... 21 more
Caused by: java.io.EOFException
at java.io.DataInputStream.readByte(DataInputStream.java:250)
at sun.rmi.transport.StreamRemoteCall.executeCal
So the journey continued and I tried using the 32bit version of the ojdbc .dylibs and .jars but then I ran into 'wrong architecture'when trying to connect to oracle db using IntelliJ's DataSource view.
I would like to keep everything consistent by using 32-bit since most of my apps use libraries that require 32-bit JVM.
tl;dr 'ers:
-Cannot connect to oracle db through IntelliJ Idea 12 DataSource view nor connect through java app using JDBC
-BackGround Info: Mac OSX 10.7.5, Java 1.6.0_51 64-bit, IntelliJ Idea 12, Remote Oracle 11G DB
-Tried both 32-bit and 64-bit versions of Oracle Instant Client with jdbc files, no luck
Any suggestions will be greatly appreciated!
Thank you
Additional Notes: I cannot connect through sqlplus either; i get the error ORA-21561: OID generation failed. Search results only point to fixing the /etc/hosts file which does not make sense for me since the db server is on a remote server and I am connecting using a TNS entry in /etc/tnsnames.ora
===UPDATE 1===
I made some progress on connecting through the java code. Using this code:
Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
Connection conn = DriverManager.getConnection("jdbc:oracle:thin:#hostname:port/service_name", "user", "password");
but received the error:
java.sql.SQLRecoverableException: IO Error: The Network Adapter could not establish the connection
I CAN connect using this exact piece of code on my work machine through eclipse ide but not on my Mac laptop.
For Intellij/DataGrip 2018 DB connection via TNS I got
[08006][17002] IO Error: Invalid connection string format, a valid format is: "host:port:sid"
I find the problem in Oracle driver version: 11.2.0.4
I get "Test Connection > Succesful" using version 12.1.0.2
I can only encourage you to use the oracle thin jdbc client. There must be a very specific reason not to. With that you will likely have NO problems.

Java RMI UnmarshalException and AWS EC2 Server

I'm trying to implement the RMI Compute PI tutorial found on the Sun/Oracle website here. The server is located on an Amazon windows EC2 Server. The client is located on my personal computer at home. Just to keep things simple, I have disabled the firewall on the server and open all the port on the EC2 security group. I can ping the machine, have access to files, etc... My aim is just to have a basic RMI program worked on AWS. I will oversee security issues later.
Lauching the server is not a problem. And it's correctly bound to the RMI Registry. I'm using the following command to launch it :
java -Djava.security.manager -Djava.security.policy=C:\AWSTest\security.policy -Djava.rmi.server.codebase=file:/C:/AWS/Sources/sun-rmi-tutorial-server/bin/ file:/C:/AWS/Sources/sun-rmi-tutorial-common/bin/ -classpath C:\AWS\Sources\sun-rmi-tutorial-server\bin;C:\AWS\Sources\sun-rmi-tutorial-common\bin engine.ComputeEngine ec2-XX-XX-XX-XXX.ap-southeast-1.compute.amazonaws.com
The RMI registry is started on the server side.
I have declared a new security manager on the server side System.setSecurityManager(new RMISecurityManager())
I'm using a security.policy file where I'm granted all permission.
When I'm launching the client side, on my local computer, I'm ending with an UnmarshalException:
ComputePi exception: error unmarshalling return; nested exception is:
java.lang.ClassNotFoundException: engine.ComputeEngine_Stub
java.rmi.UnmarshalException: error unmarshalling return; nested exception is:
java.lang.ClassNotFoundException: engine.ComputeEngine_Stub
at sun.rmi.registry.RegistryImpl_Stub.lookup(Unknown Source)
at java.rmi.Naming.lookup(Naming.java:101)
at client.ComputePi.main(ComputePi.java:14)
Caused by: java.lang.ClassNotFoundException: engine.ComputeEngine_Stub
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:264)
at sun.rmi.server.LoaderHandler.loadClass(LoaderHandler.java:453)
at sun.rmi.server.LoaderHandler.loadClass(LoaderHandler.java:184)
at java.rmi.server.RMIClassLoader$2.loadClass(RMIClassLoader.java:637)
at java.rmi.server.RMIClassLoader.loadClass(RMIClassLoader.java:264)
at sun.rmi.server.MarshalInputStream.resolveClass(MarshalInputStream.java:216)
at java.io.ObjectInputStream.readNonProxyDesc(ObjectInputStream.java:1593)
at java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:1514)
at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1750)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1347)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:369)
... 3 more
Do you have any suggestions of what is wrong ? Why can't the client find the correct Stub ? I'm using Eclipse Juno, Genady RMI Plugin and Java 1.7.0_07.
Thanks for your help
EDIT 1 :
The Java workspace is organized around 3 projects : the Client side, the Server Side and a Common project where both the server and the client share classes.
I can read the remote registry from my local computer. The RMI plugin can connect remotely to the server and inspect the registry.
If instead of making 3 projects I put all the classes under the same project, everything is working fine... But I notice that no stubs where generated (or needed).
If, on the client side, I'm putting a reference in the classpath to the server classes (everything being build locally), i have a connection timeout error instead of the unmarshalling error.
ComputePi exception: Connection refused to host: XX.XXX.XX.XX; nested exception is:
java.net.ConnectException: Connection timed out: connect
java.rmi.ConnectException: Connection refused to host: XX.XXX.XXX; nested exception is:
java.net.ConnectException: Connection timed out: 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.invoke(UnicastRef.java:128)
at engine.ComputeEngine_Stub.executeTask(ComputeEngine_Stub.java:35)
at client.ComputePi.main(ComputePi.java:19)
Caused by: java.net.ConnectException: Connection timed out: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:69)
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:157)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:391)
at java.net.Socket.connect(Socket.java:579)
at java.net.Socket.connect(Socket.java:528)
at java.net.Socket.(Socket.java:425)
at java.net.Socket.(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)
... 5 more
You haven't deployed the stub class to the client.

Class not found when running on a node

I've configured a slave to run on my machine, the slave is connected and recognized by Jenkins.
I've configured my project to use that specific slave and I can see that it kicks into action when a job needs to be done.
After a while I receive the following error:
[Android Tunneling] $ /opt/jdk/jdk1.7.0/bin/java -cp maven3-agent.jar:/opt/maven/apache- maven-3.0.4/boot/plexus-classworlds-2.4.jar org.jvnet.hudson.maven3.agent.Maven3Main /opt/maven/apache-maven-3.0.4 /home/efi/Documents/jenkins/jenkins-cli.jar maven3-interceptor.jar 34176
Error: Could not find or load main class org.jvnet.hudson.maven3.agent.Maven3Main
ERROR: Failed to launch Maven. Exit code = 1
When running this command locally via terminal I get
Exception in thread "main" java.net.ConnectException: Connection refused
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:337)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:198)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:180)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:391)
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 org.jvnet.hudson.maven3.agent.Maven3Main.main(Maven3Main.java:128)
at org.jvnet.hudson.maven3.agent.Maven3Main.main(Maven3Main.java:63)
Any idea what might be the problem ?
Command line is looking for maven to be installed on /opt/maven/apache-maven-3.0.4 (default location for cloudbees slaves). If your local setup don't match this, you need to override tools location for this custom slave

Categories