Useful Java Tips

Common Java Commands

Java Version

$ java -version

Find java path

$ sudo find / -name java

Compile java code

$ javac <.java file name>

Run java class containing main

$ java -cp <jars separated by colon> <class along with package containing main function> 

View classes inside jar

$ jar -tf <.jar file>

Find class in collection of jars

$ for i in /usr/lib/hbase/*.jar; do jar -tvf "$i" | grep -Hsi ImportTsv && echo "$i"; done

Disassemble java class file into attribute and method signatures

$ javap <java .class file>

Generate source code (decompile) from .class or .jar

http://www.javadecompilers.com/

Display java property values.

$ java -XshowSettings:properties -version
Property settings:
    awt.toolkit = sun.lwawt.macosx.LWCToolkit
    file.encoding = UTF-8
    file.encoding.pkg = sun.io
    file.separator = /
    ftp.nonProxyHosts = local|*.local|169.254/16|*.169.254/16|localhost|*.localhost
    gopherProxySet = false
    http.nonProxyHosts = local|*.local|169.254/16|*.169.254/16|localhost|*.localhost
    java.awt.graphicsenv = sun.awt.CGraphicsEnvironment
    java.awt.printerjob = sun.lwawt.macosx.CPrinterJob
    java.class.path = .
    java.class.version = 52.0
    java.endorsed.dirs = /Library/Java/JavaVirtualMachines/jdk1.8.0_45.jdk/Contents/Home/jre/lib/endorsed
    java.ext.dirs = /Users/abulbasar/Library/Java/Extensions
        /Library/Java/JavaVirtualMachines/jdk1.8.0_45.jdk/Contents/Home/jre/lib/ext
        /Library/Java/Extensions
        /Network/Library/Java/Extensions
        /System/Library/Java/Extensions
        /usr/lib/java
    java.home = /Library/Java/JavaVirtualMachines/jdk1.8.0_45.jdk/Contents/Home/jre
    java.io.tmpdir = /var/folders/ct/qgv470fj7bq6jyjdw39dngzw0000gn/T/
    java.library.path = /Users/abulbasar/Library/Java/Extensions
        /Library/Java/Extensions
        /Network/Library/Java/Extensions
        /System/Library/Java/Extensions
        /usr/lib/java
        .
    java.runtime.name = Java(TM) SE Runtime Environment
    java.runtime.version = 1.8.0_45-b14
    java.specification.name = Java Platform API Specification
    java.specification.vendor = Oracle Corporation
    java.specification.version = 1.8
    java.vendor = Oracle Corporation
    java.vendor.url = http://java.oracle.com/
    java.vendor.url.bug = http://bugreport.sun.com/bugreport/
    java.version = 1.8.0_45
    java.vm.info = mixed mode
    java.vm.name = Java HotSpot(TM) 64-Bit Server VM
    java.vm.specification.name = Java Virtual Machine Specification
    java.vm.specification.vendor = Oracle Corporation
    java.vm.specification.version = 1.8
    java.vm.vendor = Oracle Corporation
    java.vm.version = 25.45-b02
    line.separator = \n 
    os.arch = x86_64
    os.name = Mac OS X
    os.version = 10.11.4
    path.separator = :
    socksNonProxyHosts = local|*.local|169.254/16|*.169.254/16|localhost|*.localhost
    sun.arch.data.model = 64
    sun.boot.class.path = /Library/Java/JavaVirtualMachines/jdk1.8.0_45.jdk/Contents/Home/jre/lib/resources.jar
        /Library/Java/JavaVirtualMachines/jdk1.8.0_45.jdk/Contents/Home/jre/lib/rt.jar
        /Library/Java/JavaVirtualMachines/jdk1.8.0_45.jdk/Contents/Home/jre/lib/sunrsasign.jar
        /Library/Java/JavaVirtualMachines/jdk1.8.0_45.jdk/Contents/Home/jre/lib/jsse.jar
        /Library/Java/JavaVirtualMachines/jdk1.8.0_45.jdk/Contents/Home/jre/lib/jce.jar
        /Library/Java/JavaVirtualMachines/jdk1.8.0_45.jdk/Contents/Home/jre/lib/charsets.jar
        /Library/Java/JavaVirtualMachines/jdk1.8.0_45.jdk/Contents/Home/jre/lib/jfr.jar
        /Library/Java/JavaVirtualMachines/jdk1.8.0_45.jdk/Contents/Home/jre/classes
    sun.boot.library.path = /Library/Java/JavaVirtualMachines/jdk1.8.0_45.jdk/Contents/Home/jre/lib
    sun.cpu.endian = little
    sun.cpu.isalist = 
    sun.io.unicode.encoding = UnicodeBig
    sun.java.launcher = SUN_STANDARD
    sun.jnu.encoding = UTF-8
    sun.management.compiler = HotSpot 64-Bit Tiered Compilers
    sun.os.patch.level = unknown
    user.country = US
    user.country.format = US_POSIX
    user.dir = /Users/abulbasar/workspace/java
    user.home = /Users/abulbasar
    user.language = en
    user.name = abulbasar
    user.timezone = 
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)

Install Oracle JDK 1.8 in CentOS

  • Download the rpm for linux 64 bit version (for example jdk-8u101-linux-x64.rpm) from oracle download site.
$ ~/Downloads
$ wget \
--no-check-certificate \
--no-cookies --header "Cookie: oraclelicense=accept-securebackup-cookie" \
http://download.oracle.com/otn-pub/java/jdk/8u112-b15/jdk-8u112-linux-x64.rpm

Install yum install the rpm

$ sudo yum localinstall <rpm name e.g.  jdk-8u101-linux-x64.rpm> -y

After installing, you can delete the rpm file.

Set JAVA_HOME in /etc/profile by adding the following line at the end of the file.

export JAVA_HOME=/usr/java/jdk1.8.0_112

JVM monitoring tools

  • jconsole
  • jvisualvm

Maven

Add Repo

$ sudo wget http://repos.fedorapeople.org/repos/dchen/apache-maven/epel-apache-maven.repo -O /etc/yum.repos.d/epel-apache-maven.repo

Install using yum

$ sudo yum install -y apache-maven

Verify

$ mvn -version

Example pom.xml to

  • add java 8 dependencies
  • assembly plugin
  • specify main class to create executable jar

Maven Find Dependency Tree

$ mvn dependency:tree

Maven build

$ mvn clean

$ mvn package


Location of MVN tar

https://maven.apache.org/download.cgi


References: