Skip to main content

Posts

Java - How to convert an array of bytes i.e. byte[] to String ?

Introduction In this post, we will explore how to convert an array of bytes i.e. byte[] to String in Java. For this functionality, we will use the following constructor of java.lang.String class String (byte[] bytes,  String  enc) Example import java.nio.charset.StandardCharsets; public class ByteArrayToString { public static void main(String[] args) { byte[] bytes = "a quick fox jumps over the lazy dog".getBytes(); // convert the bytes to String System.out.println("bytes = " + bytes); System.out.println( "bytes.toString() = " + bytes.toString()); String myString = new String ( bytes , StandardCharsets.UTF_8); System.out.println("myString = " + myString); } } Output bytes = [B@41a4555e bytes.toString() = [B@41a4555e myString = a quick fox jumps over the lazy dog Conclusion In this post, we learned how to convert an array of bytes to String.

Java - How to convert String to InputStream?

Introduction In this post, we will learn how to convert String to InputStream in Java. Example import java.io.ByteArrayInputStream; import java.io.InputStream; public class StringToInputStream { public static void main(String[] args) { String text = "A quick fox jumps over a lazy dog"; try ( InputStream inputStream = convertToInputStream(text) ) { //TODO you business logic goes here } catch (Exception e) { e.printStackTrace(); } } private static InputStream convertToInputStream(String text) throws Exception { return new ByteArrayInputStream(text.getBytes()); } } Conclusion This is the simplest and shortest way to convert a String to InputStream in Java.

Oracle 12 C - JDBC connection string to connect through Service Name

Introduction Mostly we make a connection to Oracle using JDBC through SID. Recently I have to make a connection to Oracle using Service Name. I am writing this post to share my experience. SID Connection String  jdbc:oracle:thin:@IP:1521:SID Service Name connection String  jdbc:oracle:thin:@(description=(address=(host=IP)(protocol=tcp)(port=1521))(connect_data=(service_name=UR_SERVICE_NAME))) Replace Service Name and host address per your configuration These strings work both for plain JDBC and hibernate. For hibernate configuration, put the above string inside <property name="connection.url">Above connection String</property> tag of hibernate.cfg.xml. Conclusion In this post, we learn how to connect to Oracle using both SID and Service Name. Happy learning

Java - How to convert InputStream to String?

Introduction In this tutorial, we will learn how to convert InputStream to String in Java. Example import java.io.InputStream; import java.net.URL; public class InputStreamToString { public static void main(String[] args) { //very frequent asked question in interview , regarding closable interface try(InputStream inputStream = new URL("https://www.oracle.com/index.html").openStream()){ System.out.println(convertToString(inputStream)); }catch (Exception e) { // TODO: handle exception } } private static String convertToString(InputStream inputStream) throws Exception{ return new String(inputStream.readAllBytes()); } } Conclusion Above example is the simplest method to convert an Inpustream to String in Java. There are alternate solutions available like using commonio jar and converting InputStream to ByteStream etc. Hope you enjoy this tutorial. Happy learning 

Java - How to escape % in String.format?

Introduction In this tutorial, we will learn how to skip or display % in a String using String.format MyStringFormatter .java  public class MyStringFormatter { public static void main(String[] args) { System.out.println(String.format( "Your score is %d % " ,65) ); } } Output Exception in thread "main" java.util.UnknownFormatConversionException: Conversion = ' ' at java.base/java.util.Formatter.checkText(Formatter.java:2732) at java.base/java.util.Formatter.parse(Formatter.java:2718) at java.base/java.util.Formatter.format(Formatter.java:2655) at java.base/java.util.Formatter.format(Formatter.java:2609) at java.base/java.lang.String.format(String.java:2897) at MyStringFormatter.main(MyStringFormatter.java:4) Solution To display a % in String when using String.format , you have to escape a % with % i.e. %% public class MyStringFormatter { public static void main(String[] args) { System.out.println(String.fo

spark-submit java.lang.NoClassDefFoundError: scala/runtime/java8/JFunction1$mcII$sp

Exception  Exception in thread "main" java.lang.BootstrapMethodError: java.lang.NoClassDefFoundError: scala/runtime/java8/JFunction1$mcII$sp         at SparkPi$.main(SparkPi.scala:14)         at SparkPi.main(SparkPi.scala)         at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)         at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) Problem: It seems that you have compiled and generated your jar file with an upper version of the Scala compiler than the one spark is using. Solution Step 1 Run the following command from spark-installed-directory\bin spark-shell.cmd (or.sh) and note the Scala version, My Spark version was 2.4.3 and Scala version 2.11.12 Step 2 Change the scala version into your build.sbt to 2.11.12 (per your configuration). My build.sbt is name := "SparkPi Project" version := "1.0" scalaVersion := "2.11.12" val sparkVersion = "2.4.5&qu

Set default JAVA_HOME path for Apache Spark

Introduction  Currently, Apache Spark does not support JAVA 11 and later versions. When you try to run the Spark application, you may get the following exception Exception pyspark.sql.utils.IllegalArgumentException: 'Unsupported class file major version 55' Solution There are different ways to fix this exception like Set environmental variable for JAVA_HOME Modify Apache spark environment configuration file i.e. spark-env.sh or spark-env.cmd In this post, I will help you to set JAVA_HOME using Spark's configuration file Windows Environment  Go to the spark-directory\ conf Create a file by the name of spark-env.cmd  Paste the following line spark-env.cmd                   set JAVA_HOME=C:\Program Files\Java\jdk1.8.0_201 Linux and Mac Go to the spark-directory\ conf Open spark-env.sh Paste the following line spark-env.cmd        export JAVA_HOME=$(user/Java/jdk1.8.0_201 -v 1.8) Note : Change the installed Java directory accordingly.

JDBC – PreparedStatement– How to Set Null value?

Introduction Java Database Connection (JDBC) API allows the Java programmers to access different relational and NoSQL databases like Oracle, MySQL, SQL Server, etc. It helps to store, access and manipulate the data stored in these databases. In this post, we will explore how to set the NULL values in PreparedStatement. Null String data type For String data type, it does not matter if the value to be set as a parameter in the SQL query is NULL.   Other data types like Integer, Float, Double, Date, etc. For data types like Integer , Float , Double , Date we have to explicitly call the setNull method. Example String name = null ; Long id = null ; PreparedStatement ps = connection.prepareStatement("select * from person where first_name = ? and id = ?");  ps.setString(1,name); The above line will execute without throwing any exception ps.setLong(2, id); In the above line of code, since id is null, it will throw java.lang.NullPoi

Apache Maven - How to set network proxy settings?

In the following post, we learned how to install Apache Maven on Windows 10 How to install Apache Maven on Windows? If your development PC is behind a proxy network, you will not be able to add Java dependencies through Maven. Luckily Maven provides us with proxy settings. Add your network proxy to Maven's proxy settings STEP 1:  Navigate to your maven's directory and open conf folder E:\apache-maven-3.6.2\conf STEP 2:  Open settings.xml in your favorite text editor STEP 3: Uncomment the proxy tag inside proxies tag and add the values according to your network's proxy settings. <proxy>       <active>true</active>       <protocol>http</protocol>       <username>proxyuser</username>       <password>proxypass</password>       <host>proxy.host.net</host>       <port>80</port>       <nonProxyHosts>local.net|some.host.com</nonProxyHosts>     </proxy>

How to install Apache Maven on Windows 10?

Apache Maven is a Java tool that helps in building and managing the Java project. It is based on the concept of Project Object Modle(POM). It helps the Java developers to manage the project's JAR files in a central repository and those JAR files can be shared across the project. Maven project has the following objectives makes the build process easy provides a uniform build system  and many more In this post, we will learn how to install the Apache Maven on Windows (10). STEP 1: Download and install Java Download Java from  https://www.java.com/en/download/ . and install it. STEP 2: Download Apache Maven Download Apache Maven from  https://maven.apache.org/download.cgi  and extract it on your local machine like I have unzipped it to  E:\apache-maven-3.6.2 STEP 3: Set the JAVA_HOME environmental variable Right Click This PC ->  Properties -> Advanced system settings -> Environmental Variables Open Environmental Variables Click on N