What is JVM , JRE and JDK?

JVM :

  1. The Java Virtual Machine is the virtual machine that runs the bytecodes i.e. it coverts the byte code to machine code depending on the underlying operating system and hardcore combination.
  2. JVM doesn’t understand java typo, that’s why we compile *.java to generate *.class that contains the bytecode, which JVM can understand.
  3. JVM provides a virtual machine interface which doesn’t depend on the underlying operating system and hardware combination. So, it makes the java language as portable language i.e. write once, run anywhere.
  4. JVM is responsible for things like gc, etc…
  5. When we type command to run a java program i.e.  java followed by program name then a separate instance of JVM will be created for that program with its own class-loading mechanism. When java program or application completes, the JVM instance dies.
  6. If you start another program by calling Runtime.exec()in your current program, and that called program is java program, then a new process will be created with its own JVM instance with its own class-loading mechanism.
  7. Only one to one relation exists between process and JVM.
  8. Webapp confusion – A webapp (by example) is simply a bunch of classes and objects instantiated, attending some URL in a web server. You can start tomcat with 10 different apps. It is , 10 different bunches of classes and objects each of them attending different request, but in fact they share the JVM instance. A webapp cannot touch other webapp’s objects because nobody gives it a reference to other objects and classes, this is controlled by class-loading mechanism.

JRE :

  1. The Java Runtime Environment provides the libraries, the JVM (JRE is the implementation of JVM) and other components to run applets and applications written in java programming language.
  2. JRE doesn’t contain tools like compliers and debugger for developing applets and applications.

JDK :

  1. The Java Development Toolkit is a superset of the JRE, and contains everything this is in the JRE, plus tools such as compiler, API and debuggers necessary for developing applets and applications.
  2. JDK generates the byte code when a java program is compiled.

The following images depicts what was discussed above.

 JDK - JRE-JVM

Generating a simple XML file in java

import java.io.File;
import org.w3c.dom.*;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.ParserConfigurationException;

import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException; 
import java.io.*;
import org.w3c.dom.*;
import javax.xml.parsers.*;
import javax.xml.transform.*;
import javax.xml.transform.dom.*;
import javax.xml.transform.stream.*;


public class GeneratingSampleXml {


	public static void main(String args[]) throws Exception{
		
		/*Creating an empty document*/
		DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
		DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
		Document doc = documentBuilder.newDocument();
		
		/*Creating an element. By default first element is the root element */
		Element root = doc.createElement("Book");
		doc.appendChild(root);
		
		/* Adding a comment */
		Comment comment = doc.createComment("a comment");
		doc.insertBefore(comment, root);
		
		/* Adding Child to the root element*/
		Element title = doc.createElement("Title");
		root.appendChild(title);
		title.insertBefore(doc.createTextNode("Java Performance Tuning"), title.getFirstChild());

		Element author = doc.createElement("Author");
		root.appendChild(author);
		author.insertBefore(doc.createTextNode("Ravleen kaur"), author.getFirstChild());

		
		TransformerFactory transfac = TransformerFactory.newInstance();
		Transformer trans = transfac.newTransformer();
		trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
		trans.setOutputProperty(OutputKeys.INDENT, "yes");


		
		StringWriter sw = new StringWriter();
		StreamResult result = new StreamResult(sw);
		DOMSource source = new DOMSource(doc);
		trans.transform(source, result);
		String xmlString = sw.toString();

		
		System.out.println("Here's the xml:\n\n" + xmlString);
	}
}

JConsole to monitor and manage JVM

  • Jconsole is a JMX-compliant monitoring tool.
  • It can be used to monitor both local JVM (the same box on which jconsole is running) and remote (Other boxes) JVM on which the specified application is running that need to be monitored.
  • Jconsole allows to take the dump of the observations and you can use eclipse-mat to analyze the dump in more detailed.

    For more details on Jconsole you can refer to the following links

      Maven : Error occurred during initialization of VM

      issue : when we are trying to build a maven project from command prompt, we were encountered with the following error message

      “Error occurred during initialization of VM
      Could not reserve enough space for object heap
      Could not create the Java virtual machine.”

      Solution 1 : when we reduced the memory in maven options, we were able to build the project. Changed maven options are given below.

      MAVEN_OPTS = -Xmx512m -XX:MaxPermSize=128m

      Issue : “OutOfMemoryError” while building a maven project

      Issue : when we are trying to build a maven project, we encountered the following error

      “java.lang.OutOfMemoryError: Java heap space
      at com.sun.tools.javac.util.Name.fromUtf(Name.java:84)
      at com.sun.tools.javac.util.Name$Table.fromUtf(Name.java:510)
      at com.sun.tools.javac.util.ByteBuffer.toName(ByteBuffer.java:151)
      at com.sun.tools.javac.jvm.ClassWriter.typeSig(ClassWriter.java:407)
      at com.sun.tools.javac.jvm.ClassWriter.writeMethod(ClassWriter.java:945)
      at com.sun.tools.javac.jvm.ClassWriter.writeMethods(ClassWriter.java:1426)
      at com.sun.tools.javac.jvm.ClassWriter.writeClassFile(ClassWriter.java:1507)
      at com.sun.tools.javac.jvm.ClassWriter.writeClass(ClassWriter.java:1444)
      at com.sun.tools.javac.main.JavaCompiler.genCode(JavaCompiler.java:618)
      at com.sun.tools.javac.main.JavaCompiler.generate(JavaCompiler.java:1289)
      at com.sun.tools.javac.main.JavaCompiler.generate(JavaCompiler.java:1259)
      at com.sun.tools.javac.main.JavaCompiler.compile2(JavaCompiler.java:765)
      at com.sun.tools.javac.main.JavaCompiler.compile(JavaCompiler.java:730)
      at com.sun.tools.javac.main.Main.compile(Main.java:353)
      at com.sun.tools.javac.main.Main.compile(Main.java:279)
      at com.sun.tools.javac.main.Main.compile(Main.java:270)
      at com.sun.tools.javac.Main.compile(Main.java:69)
      at com.sun.tools.javac.Main.main(Main.java:54)”.

      Solution 1 : As a quick solution add the following statements to pom.xml

      <plugin>
      <artifactId>maven-compiler-plugin</artifactId>
      <version>2.0.2</version>
      <configuration>
      <source>${compile.src.version}</source>
      <target>${compile.src.version}</target>
      <debug>true</debug>
      <optimize>true</optimize>
      <meminitial>128m</meminitial>
      <maxmem>1024m</maxmem>
      <fork>true</fork>
      <showDeprecation>true</showDeprecation>
      <showWarnings>true</showWarnings>
      </configuration>
      </plugin>

      Note : if the same problem exists from eclipse then try running from command prompt.

      Find process id and task on windows

      The following are the commands for finding a process id on windows

      1. To find open port and their PIDs go to command prompt and type the follwing command — “Netstat -a -n -o” .

      2. To get the task from PID type the following command – ” tasklist /svc /FI “PID eq 1304″ ” .

      Note : there is space between tasklist and /svc. similarly there is a space between svc and /FI.