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 […]
[Git] How to delete tag in Git
There are some situations in which you would like to delete a tag in git like missing some critical update in tag. It’s not recommended that you delete a tag, but sometimes it comes in handy Let’s see how to delete a tag. Suppose you have a tag Release-1.0 and want to delete it $git […]
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 […]
What are Closures and Lambdas in PHP?
What are Closures and Lambdas in PHP? Closures and Lambdas were added into PHP in 5.3 release. Since then we all are using them knowingly or unknowingly in our day-to-day PHP programming. Have you ever wonder how it works when you pass a function as a callback to array_map or array_walk. They are all Lambdas […]
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 […]