It’s common to get pid or process id using top or ps command. Sometime we have a need that get the pid from within java code. How do we do it? JMX comes to the rescue. The information is available as part RuntimeMXBean. Following is the code snippet that can get the pid public static […]
Java
How to convert StackTrace to String
We are all used to print stacktraces to console or in log files. At times we have a need to convert Stacktrace to String to send to another logging system or for related purpose. Let’s see what are the options available to achieve this Option 1 : Using a PrintWriter
1 2 3 4 5 6 |
public static String getStackTraceAsString(Throwable cause) { StringWriter exceptionWriter = new StringWriter(); PrintWriter writer = new PrintWriter(exceptionWriter); cause.printStackTrace(writer); return exceptionWriter.toString(); } |
The printStackTrace() API takes […]
How to execute native commands and external programs from Java
Many time we need to use native commands or other programs like Perl or Python scripts from Java. In this post let us explore how can we achieve this. To explain we shall use simple ls command and print it’s output. We can execute external programs using Runtime class. Let’s see the code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
import java.io.BufferedReader; import java.io.InputStreamReader; /** * Simple Process runner */ public class ProcessRunner { public static void main(String[] args) throws Exception { Runtime runtime = Runtime.getRuntime(); Process myProcess = runtime.exec("ls /tmp"); BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(myProcess.getInputStream())); String line; while((line = bufferedReader.readLine()) != null) { System.out.println(line); } myProcess.waitFor(); } } |
The […]
How to read web pages using Java?
In earlier posts we have seen how to read text files and binary files in Java. But how to read a remote resource like a web page in Java. Let’s see how to do it. Reading a web page Reading a remote web page is quite similar to the way we have read a text […]
How is Hashtable different from HashMap?
In the last post we saw how HashMap works. Hashtable is another Map implementation which is commonly used. In this post let’s briefly touch upon how it works and see some major differences the two implementations have. Working of Hashtable The working of Hashtable is quite similar to HashMap. It also follows the same process […]
How to compile JDK API source with debug info?
JDK API’s are compiled without debug info to save of space requirements, which means when debugging inside them the debugger cannot find local variable information to show during the debugging session. It’s quite easy to compile the source and create a similar rt.jar to be used for debugging. Let’s see the steps. Option 1: Add […]
How HashMap works in Java?
HashMap is an implementation of Map interface, which maps keys to values. A Map cannot contain duplicate keys. We know we can put and get Key-values into HashMap. So, how does HashMap actually works in Java. This post explores a bit about the internal working. Before we jump into details, let’s see some basic terminology […]
How to enable JDK classes debugging in IntelliJ Idea
Debugging a Java Application could be fun and using debugging to explore JDK classes is even more fun. This helps in exploring the internals of JDK as well as broadens ones perspective of how things work. Unfortunately, stepping into JDK classes during debugging is disabled by default. Let’s see how to enable stepping into JDK […]
Using Google Guava Preconditions to validate Arguments
Validating method input arguments is a need for all API’s and we need to write the glue code every time we do validations. In a nutshell, the validation finally results into a boolean result that we take some action on. Google Guava provides a nice way of validating the parameters in simple and concise way. […]
How to analyse Heap dumps using Eclipse MAT
We have seen in previous article about how to generate Heap dumps. Once we have heap dump, we need analyse them to find out possible cause of OOME. Let’s see how to analyse Heap dumps using Eclipse MAT For this we would need a Heap dump. Let’s create a simple program that generates Heap dump […]