InetAddress.getLocalHost() failing on machine running embedded Java - java

In this post java H2 hanging on getLocalhost on arm32 device I had a problem creating database using Hibernate. I have now managed to remove track down the issue to core java with this test program, InetAddress lookup using getByName("localhost") works, but getLocalHost() fails !
Points towards configuration issue with linux machine. localhost is in /etc/hosts
But I am no Linux expert, what could cause this ?
import java.net.InetAddress;
import java.net.UnknownHostException;
public class Addresses
{
public static void main(String[] args) {
try {
InetAddress address= InetAddress.getByName("localhost");
System.out.println(address.getHostName()+ "-"+address.getHostAddress());
address= InetAddress.getLocalHost();
System.out.println(address.getHostName()+ "-"+address.getHostAddress());
} catch (UnknownHostException e) {
e.printStackTrace();
}
}
}
Gives
localhost-127.0.0.1
java.net.UnknownHostException: N1-ZS10: N1-ZS10: Name or service not known
at java.net.InetAddress.getLocalHost(InetAddress.java:1505)
at Addresses.main(Addresses.java:12)
Caused by: java.net.UnknownHostException: N1-ZS10: Name or service not known
at java.net.Inet4AddressImpl.lookupAllHostAddr(Native Method)
at java.net.InetAddress$2.lookupAllHostAddr(InetAddress.java:928)
at java.net.InetAddress.getAddressesFromNameService(InetAddress.java:1323)
at java.net.InetAddress.getLocalHost(InetAddress.java:1500)
... 1 more

Related

Getting exception NoClassDefFoundError when connecting to Redis with lettuce 6.0.1

I'm new to java. Trying to connect to Redis with Lettuce.
Environment: openjdk - 15.0.1, Lettuce-core-6.0.1. RELEASE, Redis instance launched on RedisLabs (free tier), Dependency management as java Plain. IDE: IttelijIDEA.
I wrote this program code bellow, and when building its raises an Exception. Kindly asking for any suggestions how that could be fixed? Thanks a lot in advance to provided assistance.
import io.lettuce.core.RedisClient;
import io.lettuce.core.api.StatefulRedisConnection;
import io.lettuce.core.api.sync.RedisCommands;
public class redisConnect {
public static void main(String[] args) {
System.out.println("I remember how to create simple java program :)");
System.out.println("with this program I will connect to REDIS instance on ReidsLabs");
RedisClient redisClient = RedisClient.create("redis://password#host-name-from-redisLabs:port/dbname");
try (StatefulRedisConnection<String, String> connection = redisClient.connect()) {
RedisCommands<String, String> syncCommands = connection.sync();
syncCommands.set("key", "Hello, Redis!");
String str = syncCommands.get("key");
System.out.println(str);
connection.close();
}
redisClient.shutdown();
}
}
Exception value:
Exception in thread "main" java.lang.NoClassDefFoundError: io/netty/channel/group/ChannelGroup
at redisConnect.main(redisConnect.java:11)
Caused by: java.lang.ClassNotFoundException: io.netty.channel.group.ChannelGroup
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:606)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:168)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522)
... 1 more
Process finished with exit code 1
Lettuce Client has been uploaded from here: https://github.com/lettuce-io/lettuce-core/releases/download/6.0.1.RELEASE/lettuce-core-6.0.1.RELEASE-bin.zip
Code example has been taken from here: https://lettuce.io/core/6.0.1.RELEASE/reference/#getting-started.plain-java
Dependency to the project in intellij has been imported: like that:

SSL error when connecting to remote SQL server using Java (netbeans)

i asked this question before but got no answers, i think mainly because it was a mess, i'll state the facts and put some quotes from the code under
im using VMWare, and have one Windows Server 2003 with SQL Server 2005 on it, with a SQL Login, and it has a DNS Service running
i also have a windows 7 machine running NetBeans8.2 with JDK 8.1, using JDBC 4.2
i can connect to the server using SQL Manager from the Windows 7 machine
i can't connect using the java code because of an SSL Error, i am not sure what is causing it
this is for a school project so i must use SQL server 2005
here's my connection code :
package connectbd;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.PreparedStatement;
public class ConnectBD {
public static void main(String[] args) {
String jdbcurl;
Connection con = null;
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
jdbcurl = "jdbc:sqlserver://SQLSERVER;instanceName=SQLE;user=****;password=****;database=LP_SIBD_GR15";
try {
con=DriverManager.getConnection(jdbcurl);
System.out.println("Connection success");
} catch(SQLException e) {
e.printStackTrace();
}
}
}
This is the error that i get :
com.microsoft.sqlserver.jdbc.SQLServerException: The driver could not establish a secure connection to the SQL Server using Secure Sockets Layer (SSL) encryption. Error: "The SQL Server server returned no response. The connection has been closed. ClientConnectionId:e9655c34-7c66-42c8-aaec-36601b53ff98 ».
at com.microsoft.sqlserver.jdbc.SQLServerConnection.terminate(SQLServerConnection.java:2826)
****at com.microsoft.sqlserver.jdbc.TDSChannel.enableSSL(IOBuffer.java:1829)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectHelper(SQLServerConnection.java:2391)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.login(SQLServerConnection.java:2042)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectInternal(SQLServerConnection.java:1889)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connect(SQLServerConnection.java:1120)
at com.microsoft.sqlserver.jdbc.SQLServerDriver.connect(SQLServerDriver.java:700)
at java.sql.DriverManager.getConnection(DriverManager.java:664)
at java.sql.DriverManager.getConnection(DriverManager.java:270)
at connectbd.ConnectBD.main(ConnectBD.java:35)
Caused by: java.io.IOException:
The SQL Server server returned no response. The connection was closed. ClientConnectionId:e9655c34-7c66-42c8-aaec-36601b53ff98
****at com.microsoft.sqlserver.jdbc.TDSChannel$SSLHandshakeInputStream.ensureSSLPayload(IOBuffer.java:786)
at com.microsoft.sqlserver.jdbc.TDSChannel$SSLHandshakeInputStream.readInternal(IOBuffer.java:836)
at com.microsoft.sqlserver.jdbc.TDSChannel$SSLHandshakeInputStream.read(IOBuffer.java:829)
at com.microsoft.sqlserver.jdbc.TDSChannel$ProxyInputStream.readInternal(IOBuffer.java:999)
at com.microsoft.sqlserver.jdbc.TDSChannel$ProxyInputStream.read(IOBuffer.java:989)
at sun.security.ssl.InputRecord.readFully(InputRecord.java:465)
at sun.security.ssl.InputRecord.read(InputRecord.java:503)
at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:983)
at sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1385)
at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1413)
at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1397)
at com.microsoft.sqlserver.jdbc.TDSChannel.enableSSL(IOBuffer.java:1756)
... 8 more
BUILD SUCCESSFUL (total time: 1 second)
i believe the causes are somewhere in these 2 lines: the ones marked by **** at the start
i have to deliver a webservices project for school in 2 weeks, and this is kind of a huge obstacle, any quick help or suggestion would be very appreciated
You will need to add few parameters to specify the ssl connection, such as integratedSecurity=true, encrypt=true and trustServerCertificate=true.
jdbcurl = "jdbc:sqlserver://SQLSERVER;instanceName=SQLE;user=****;password=****;database=LP_SIBD_GR15;integratedSecurity=true;encrypt=true;trustServerCertificate=true";

NetworkInterface.getNetworkInterfaces() not work on JDK 1.7 OS X?

Running the following code, I got a SocketException:
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Collections;
import java.util.Enumeration;
public class interfaces {
public static void main(String[] args) throws SocketException {
Enumeration<NetworkInterface> inters = NetworkInterface.getNetworkInterfaces();
for (NetworkInterface iface : Collections.list(inters)) {
System.out.println(iface.getDisplayName());
}
}
}
I'm using OS X 10.8.2 with JDK 1.7.0_09-b05.
It works well on Windows with THIS JDK VERSION, and also well on OS X with JDK 1.6.
Can anybody help me to test this and check if it's a bug of JDK?
Here is the stack trace:
Exception in thread "main"
java.net.SocketException: Can't assign requested address
at java.net.NetworkInterface.getAll(Native Method)
at java.net.NetworkInterface.getNetworkInterfaces(NetworkInterface.java:334)
I have a similar issue that was caused by an ipv6 address being returned from java.net.NetworkInterface.getDefault(). I'm on a Macbook and was using wireless -- p2p0 (used for AirDrop) was returned as the default but mine only has an ipv6 ether entry when running ipconfig (no inet).
Two solutions, both of which worked for me (I prefer the first)
Start your JVM with -Djava.net.preferIPv4Stack=true. This caused java.net.NetworkInterface.getDefault() to return my vboxnet0 network interface -- not sure what you'll get if you're not running a host-only VM.
Turn off wireless and use a wired connection

Rmi connection refused with localhost

I have a problem using java rmi:
When I'm trying to run my server, I get a connectException (see below).
Exception happens when executing the rebind method:
Runtime.getRuntime().exec("rmiregistry 2020");
MyServer server = new MyServer();
Naming.rebind("//localhost:2020/RemoteDataPointHandler", server);
when using rmi://localhost:2020/RemoteDataPointHandler instead, it doesn't work either. Also using the default port does not work. I also tried using the 127.0.0.1 ip-address, but with the same effect.
my runtime args:
-Djava.security.policy=java.security.AllPermission
Exception in thread "main" 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:574)
at sun.rmi.transport.tcp.TCPChannel.createConnection(TCPChannel.java:185)
at sun.rmi.transport.tcp.TCPChannel.newConnection(TCPChannel.java:171)
at sun.rmi.server.UnicastRef.newCall(UnicastRef.java:306)
at sun.rmi.registry.RegistryImpl_Stub.rebind(Unknown Source)
at java.rmi.Naming.rebind(Naming.java:160)
at be.fortega.knx.server.Main.(Main.java:25)
at be.fortega.knx.server.Main.main(Main.java:16)
Caused by: java.net.ConnectException: Connection refused
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:333)
at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:195)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:182)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:433)
at java.net.Socket.connect(Socket.java:524)
at java.net.Socket.connect(Socket.java:474)
at java.net.Socket.(Socket.java:371)
at java.net.Socket.(Socket.java:184)
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:569)
... 7 more
had a simliar problem with that connection exception. it is thrown either when the registry is not started yet (like in your case) or when the registry is already unexported (like in my case).
but a short comment to the difference between the 2 ways to start the registry:
Runtime.getRuntime().exec("rmiregistry 2020");
runs the rmiregistry.exe in javas bin-directory in a new process and continues parallel with your java code.
LocateRegistry.createRegistry(2020);
the rmi method call starts the registry, returns the reference to that registry remote object and then continues with the next statement.
in your case the registry is not started in time when you try to bind your object
It seems to work when I replace the
Runtime.getRuntime().exec("rmiregistry 2020");
by
LocateRegistry.createRegistry(2020);
anyone an idea why? What's the difference?
You need to have a rmiregistry running before attempting to connect (register) a RMI service with it.
The LocateRegistry.createRegistry(2020) method call creates and exports a registry on the specified port number.
See the documentation for LocateRegistry
One difference we can note in Windows is:
If you use Runtime.getRuntime().exec("rmiregistry 1024");
you can see rmiregistry.exe process will run in your Task Manager
whereas if you use Registry registry = LocateRegistry.createRegistry(1024);
you can not see the process running in Task Manager,
I think Java handles it in a different way.
and this is my server.policy file
Before running the the application, make sure that you killed all your existing
javaw.exe and rmiregistry.exe corresponds to your rmi programs which are
already running.
The following code works for me by using Registry.LocateRegistry() or
Runtime.getRuntime.exec("");
// Standard extensions get all permissions by default
grant {
permission java.security.AllPermission;
};
VM argument
-Djava.rmi.server.codebase=file:\C:\Users\Durai\workspace\RMI2\src\
Code:
package server;
import java.rmi.Naming;
import java.rmi.RMISecurityManager;
import java.rmi.Remote;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
public class HelloServer
{
public static void main (String[] argv)
{
try {
if(System.getSecurityManager()==null){
System.setProperty("java.security.policy","C:\\Users\\Durai\\workspace\\RMI\\src\\server\\server.policy");
System.setSecurityManager(new RMISecurityManager());
}
Runtime.getRuntime().exec("rmiregistry 1024");
// Registry registry = LocateRegistry.createRegistry(1024);
// registry.rebind ("Hello", new Hello ("Hello,From Roseindia.net pvt ltd!"));
//Process process = Runtime.getRuntime().exec("C:\\Users\\Durai\\workspace\\RMI\\src\\server\\rmi_registry_start.bat");
Naming.rebind ("//localhost:1024/Hello",new Hello ("Hello,From Roseindia.net pvt ltd!"));
System.out.println ("Server is connected and ready for operation.");
}
catch (Exception e) {
System.out.println ("Server not connected: " + e);
e.printStackTrace();
}
}
}
it seems that you should set your command as an String[],for example:
String[] command = new String[]{"rmiregistry","2020"};
Runtime.getRuntime().exec(command);
it just like the style of main(String[] args).

Connecting error through javamail

I'm trying to connect to james server using imap protocol, but I'm getting following exception:
Exception in thread "main" javax.mail.MessagingException: Network is unreachable: connect;
nested exception is:
java.net.SocketException: Network is unreachable: connect
at com.sun.mail.imap.IMAPStore.protocolConnect(IMAPStore.java:611)
at javax.mail.Service.connect(Service.java:291)
at javax.mail.Service.connect(Service.java:172)
at mail.main(mail.java:112)
Caused by: java.net.SocketException: Network is unreachable: connect
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:333)
at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:195)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:182)
at java.net.SocksSocketImpl.connect(PlainSocketImpl.java:366)
at java.net.Socket.connect(Socket.java:519)
at java.net.Socket.connect(Socket.java:469)
at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:267)
at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:277)
at com.sun.mail.iap.Protocol.<init>(Protocol.java:107)
at com.sun.mail.imap.protocol.IMAPProtocol.<init>(IMAPProtocol.java:103)
at com.sun.mail.imap.IMAPStore.protocolConnect(IMAPStore.java:578)
... 3 more
The James Server is already running, I don't get the reason of the above exception. Is this because James doesn't support this protocol or there any other reason?
Here's the source code of Javamail application, which is trying to connect to James Server:
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Properties;
import javax.mail.*;
import javax.mail.internet.*;
public class mail{
public static void main(String[] argts){
try {
Properties props=new Properties();
props.put("mail.host", "127.0.0.1 ");
props.put("mail.smtp.auth","true");
Session session = Session.getInstance(props, new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("blue", "blue");
}
});
int Spam=0;
Store store=session.getStore("imap");
store.connect("localhost", "red", "red");
Folder folder=store.getFolder("IMAPFolder");
Folder folder1=store.getFolder("Spam");
boolean b=folder1.create(Spam);
System.out.println(b);
} catch (Throwable t) {
t.printStackTrace();
}
}
}
}
A quick check is to see if you can talk to your IMAP server simply by using telnet:
telnet localhost 143
and if this doesn't connect, then James isn't publishing an IMAP connection (assuming the standard IMAP port).
I see from the below that you're using James 2.x. This link suggests that IMAP isn't supported.
Looks more like a network configuration error (can you ping it)
Check that 127.0.0.1 is setup correctly (and get rid of that space)

Categories