Hostname from Red Hat Enterprise Linux Server - java

I am using given below java code to get the Hostname from RHEL Using IBM JDK 1.8
import java.net.InetAddress;
public class Hostname {
public static void main(String[] args) {
try{
String hostname = InetAddress.getLocalHost().getHostName();
System.out.println("Hostname is :"+hostname);
}
catch(Exception e){
System.out.println(" Exception :"+e);
System.out.println(" Exception Msg :"+e.getMessage());
}
}
}
Hostname from RHEL 6.9 /IBM JDK 1.8 = vas2cxn00001122.
Fully qualified hostname from RHEL 7.6 / IBM JDK 1.8 = vas2cxn00003344.cloud.vd.org.
Why is there a difference between 6.9 vs 7.6? Where we have to update or modify the hostname in RHEL 7.6, in order to get only hostname, instead of fully qualified hostname.

I could not able to find out the root cause of RHEL 7.6 . So i have changed my java logic
from
InetAddress.getLocalHost().getHostName()
to
Runtime.getRuntime().exec("hostname")

Related

InetAddress java 8 is not getting the hostname

InetAddress.getLocalHost().getHostName() is no more getting the name of the HostName since I switched to java 8 ...
with the jdk1.8 the InetAddress.getLocalHost().getHostName() returns "localhost". Before (when I was using jdk1.6) it gives me the right hostname (which is "ACTION03") according to the network config :
cat /etc/sysconfig/network
NETWORKING=yes
HOSTNAME=ACTION03
any help ?
There was similar bug fired in JDK.
What I understand is that they changed default resolution process.
They honor configuration in /etc/nsswitch.conf where hosts are configured for /etc/hosts that gives it main priority for name resolution.
Usually /etc/hosts has record for 127.0.0.1 localhost that provide name for host localhost
Works for me on Linux (Ubuntu 14.04) with Java 1.8.0_05.
public class HostName {
public static void main(String[] args) throws Exception {
System.out.println(java.net.InetAddress.getLocalHost().getHostName());
}
}
robert#habanero:~$ javac HostName.java && java HostName
habanero

java.sql.SQLException: No suitable driver found for jdbc

I am attempting to run some queries using JDBC and keep getting this error:
Exception in thread "main" java.lang.IllegalStateException: error
at com.mycompany.app.App.writer(App.java:195)
at com.mycompany.app.App.main(App.java:19)
Caused by: java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/dbname
here is the relevant part of my code:
public class App {
writer();
}
public static void writer() {
String url = "jdbc:mysql://localhost:3306/dbname
String username = "root";
String password = "password";
try (Connection connection = DriverManager.getConnection(url, username, password)) {
Statement st = connection.createStatement();
ResultSet r= st.executeQuery("insert query here");
} catch (SQLException e) {
throw new IllegalStateException("error");
}
}
}
When I run it through Intellij Idea it works, but I need it to run on a server running Centos.
I have tried running it with this commands:
javac -cp "filepath/mysql-connector-java-5.1.35-bin.jar" App.java
java -cp ".filepath/mysql-connector-java-5.1.35-bin.jar" App
I have tried running it using maven, including
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.35</version>
</dependency>
in pom.xml and still get the same error
I have looked through many articles online (and stack questions) and still can't find the solution.
The server is running CentoOS 6.6 and the database is running locally.
I am using:
java version "1.8.0_45"
Java(TM) SE Runtime Environment (build 1.8.0_45-b14)
Java HotSpot(TM) 64-Bit Server VM (build 25.45-b02, mixed mode)
Entries on the classpath on Unix-like operating systems must be separated by :. Add a : between . (which indicates the current directory) and the path of the jar:
java -cp .:filepath/mysql-connector-java-5.1.35-bin.jar App
Alternatively, you may place your mysql-connector-java-5.1.35-bin.jar to the following directory -
JAVA_HOME/jre/lib/ext
If you place the jar in this directory then you don't have to add it to classpath by using command.
You can try loading the driver class using class.forName("qualified name of driver class"). This should resolve the issue if you're not facing the issue of class path with maven. Have a look at this where
the index tag was removed from pom and it worked.

sqljdbc_auth.dll: Can't load AMD 64-bit .dll on a IA 32-bit platform

I am trying to connect to SQL Server 2008 from JAVA code using JDBC sql server with IntegratedSecurity to use Windows Authentication.
SQL authentication worked fine with the code but when I use IntegratedSecurity for Windows Authentication, I am facing sql driver issues. I have provided the brief scenario below.
Java Code
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class Sqlselection
{
public static void main(String[] args)
{
try
{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
System.out.println("This programe runs on "+ System.getProperty("java.version"));
String url="jdbc:sqlserver://*****\\*****;integratedSecurity=true";
//Masked Server name & DB name
Connection con = DriverManager.getConnection(url);
Statement s1 = con.createStatement();
ResultSet rs = s1.executeQuery("SELECT count(1) myrecords FROM dbo.mytable");
String[] result = new String[20];
if(rs!=null){
while (rs.next()){
for(int i = 0; i <result.length ;i++)
{
for(int j = 0; j <result.length;j++)
{
result[j]=rs.getString(i);
System.out.println(result[j]);
}
}
}
}
} catch (Exception e)
{
e.printStackTrace();
}
}
}
Error while running the code
WARNING: Failed to load the sqljdbc_auth.dll cause : E:\POC\OMSChecker\sqljdbc_auth.dll: Can't load AMD 64-bit .dll on a IA 32-bit platform
*com.microsoft.sqlserver.jdbc.SQLServerException:* This driver is not configured for integrated authentication. ClientConnectionId:27af9d19-d144-47be-b9cf-bf646ed9bb3f
Issue root cause
sqljdbc_auth.dll is not compatible with current platform.
System properties
C:\Users>java -version
java version "1.6.0_25"
Java(TM) SE Runtime Environment (build 1.6.0_25-b06)
Java HotSpot(TM) 64-Bit Server VM (build 20.0-b11, mixed mode)
Eclipse properties
launcher: win32.x86_64_1.1.200.v20120913-144807
Used dll: E:\sqljdbc_2.0.1803.100_enu.exe\sqljdbc_2.0\enu\auth\x64\sqljdbc_auth.dll
Added External Jar: sqljdbc4.jar
Native library location: "E:\sqljdbc_2.0.1803.100_enu.exe\sqljdbc_2.0\enu\auth\x64\"
Notes
Can someone kindly help me with the fix. I am new to JAVA coding and I have tried all the solutions given for similar kind of posts.
Finally found the solution myself by re-installing Windows OS.
Used the same code above with sqljdbc4.jar & below VM argument
-Djava.library.path=E:\sqljdbc_4.0\enu\auth\x86\
I was able to connect successfully.

How to execute a java proram on a Linux machine without a script?

We are using Ubuntu machienes as our servers, such we got 14 servers.
The command ifconfig works on all terminals it gives specific IP address.
When I ran the following java program through a script, on one of the 14 servers I get the host ip as standard 127.0.0.1 (I uploaded this file and script on only 2 machines as of now)
package com;
import java.net.Inet4Address;
public class IpAddressTest {
public static void main(String args[]) throws Exception {
String ipaddress = Inet4Address.getLocalHost().getHostAddress();
String hostname = Inet4Address.getLocalHost().getHostName();
System.out.println("THE IP ADDRESS IS" + ipaddress);
System.out.println("THE HOST NAME IS" + hostname);
}
}
To test this program as which server will give me IP as 127.0.0.1, I need to upload this java class file and related script to execute that on all of the 14 servers.
Is there any other alternate way of finding that? I want to check if i can get the IP THROUGH java , but for this i need to upload these java file and script file to all 14 servers .so asking is there any alternate way 
First, if you want to use Java, then you have to upload the classes and the script to every server.To find all the IP addresses: Get local IP-Address without connecting to the internet

Java cannot resolve DNS address from AIX: UnknownHostException

I'm having this strange error.
On AIX, if I can reach my server from the command line ( using ping / telnet )
But If I try using java I've got UnkownHostException
This is due to Java cannot somehow "use" the DNS but I don't know why. If I use the IP address it works fine.
This is my test program.
import java.net.*;
public class Test {
public static void main( String [] args ) throws Exception {
String host = args[0];
int port = Integer.parseInt( args[1] );
System.out.println("Connecting to: " + host + " at port: " + port );
Socket socket = new Socket( host, port );
System.out.println("Connected!");
socket.close();
System.out.println("Closed!");
}
}
Is anyone aware of some kind of configuration under AIX that forbids programs ( like java ) to access DNS information?
I ( well the sysadm ) have added my address in /etc/hosts but it doesn't work either.
Thanks in advance
Java version:
Java(TM) 2 Runtime Environment, Standard Edition (build pap32dev-20080315 (SR7))
IBM J9 VM (build 2.3, J2RE 1.5.0 IBM J9 2.3 AIX ppc-32 j9vmap3223-20080315 (JIT enabled)
I am having this problem as well. I have an number of java programs installed on Ubuntu64, and none of them can resolve domain names (there are multiple JRE's too - some of them are IBM products). If I put the domain name in the hosts file with the IP address, then it works for those domains only. Every other non java program works just fine with domain resolution. WEIRD!
If I find the answer, I will post it here. If you have the answer, help us please!
Java doesn't seem to respect the ordering of DNS lookups specified on the system. For example, on my Solaris system in /etc/nsswitch.conf I have defined:
hosts: files nis dns
Java want to go to dns first, which I don't understand. It seems like it's possible to change the ordering, by setting sun.net.spi.nameservice.provider.n properties.
One workaround I've found is to append a '.' at the end of a host name. For example, if in /etc/hosts, I have
192.168.1.1 mailhost
In my java app, I would specify InetAddress.getAllByName("mailhost.").
Setting the System property
sun.net.spi.nameservice.provider.1=dns,sun
fixed this problem for me on Ubuntu
Check if you need to use a proxy and if so specify its details on the command line
http://helpdesk.objects.com.au/java/how-to-tell-a-java-application-to-use-a-proxy-server
You have to check in the /etc/services files.
I had the same error and it was because the service:
domain ..........
was commented.

Categories