EdmondFrank's 时光足迹

この先は暗い夜道だけかもしれない それでも信じて進むんだ。星がその道を少しでも照らしてくれるのを。
或许前路永夜,即便如此我也要前进,因为星光即使微弱也会我为照亮前途。
——《四月は君の嘘》

Java Dev Tools and Debugging Notes

Jave Dev tools and Debugging Notes

  1. Java
    1. Maven
      1. use maven with proxy and skip tests:
      2. Structure
    2. JShell
    3. Javap
    4. Jar Files
      1. compile and package as a FAT-JAR
      2. differences between “java -cp” and “java -jar”
    5. Debugging
      1. Exec args:
      2. Jdb

Java

Some notes about Java dev tools and debuggers

Maven

build lifecycle targets

  • validate: validate the project is correct and all necessary information is available
  • compile: compile the source code of the project
  • test: test the compiled source code using a suitable unit testing framework. There tests should not require the code be packaged or deployed
  • package: take the compiled code and package it in its distributable format, such as JAR
  • verify: run any checks on results of integration tests to ensure quality criteria are met
  • install: install the package into the local repository, for use as a dependency in other projects locally
  • deploy: done in the build environment, copies the final package to the remote repository for sharing with other developers and projects

use maven with proxy and skip tests:

-Dmaven.test.skip=true -Dhttps.proxyHost=127.0.0.1 -Dhttps.proxyPort=1081 -Dhttp.proxyHost=127.0.0.1 -Dhttp.proxyPort=1081

Structure

  1. Expected directory structure:
    • Java files are in src/main/java as well as src/test/java.
    • Resource files are under src/main/resources and src/test/resources.
  2. mvn archetype:generate: Generates a skeleton of a project based on your inputs (package name, versioning, project name, etc)
  3. Edit pom.xml and set the jdk version there..
  4. mvn package - compile, test, bundle.

JShell

Java REPL(Read Eval Print Loop) import after Java 9

  • /list -start - shows modules imported at startup.
  • /edit - edit that line in a new window.
  • /set editor “vi” - use vi instead of the default graphical edit pad.
  • /save abc.java - save current buffer to file.
  • /load abc.java - load from file into shell.
  • /-1 - execute last snippet.
  • /1 - execute first snippet.
  • /drop N - drop Nth snippet.
  • /vars - show only variables that were defined in snippets.
  • /types - show only classes that were defined in snippets.

Javap

javap TestDecompile.class - decompile .class file to human readable format. Does not show content of methods though. javap -c TestDecompile.class - show jvm bytecode in human readable -form, including methods.

Jar Files

These are zip files that have a META-INF folder with a Manifest.mf file inside.

compile and package as a FAT-JAR

<?xml version="1.0" encoding="UTF-8"?>
<build>
   <finalName>indexer-spider</finalName>
   <plugins>
      <plugin>
         <groupId>org.apache.maven.plugins</groupId>
         <artifactId>maven-assembly-plugin</artifactId>
         <version>2.4.1</version>
         <configuration>
            <!--  get all project dependencies  -->
            <descriptorRefs>
               <descriptorRef>jar-with-dependencies</descriptorRef>
            </descriptorRefs>
            <!--  MainClass in mainfest make a executable jar  -->
            <archive>
               <manifest>
                  <mainClass>org.apache.maven.indexer.examples.BasicUsageExample</mainClass>
               </manifest>
            </archive>
         </configuration>
         <executions>
            <execution>
               <id>make-assembly</id>
               <!--  bind to the packaging phase  -->
               <phase>package</phase>
               <goals>
                  <goal>single</goal>
               </goals>
            </execution>
         </executions>
      </plugin>
      <plugin>
         <groupId>org.apache.maven.plugins</groupId>
         <artifactId>maven-compiler-plugin</artifactId>
         <configuration>
            <source>8</source>
            <target>8</target>
         </configuration>
      </plugin>
   </plugins>
</build>

differences between “java -cp” and “java -jar”

  • There won’t be any difference in terms of performance.
  • java -cp: must specify the required classes and jar’s in the classpath for running a java class file.
  • java -jar: jvm finds the class that it needs to run from /META-INF/MANIFEST.MF file inside the jar file

Debugging

  • jps - Shows all runnning java processes.
  • jvisualvm - If you have it, it shows the java processes on the system with details on threads, profiler etc.
  • debugging mode - Use these args to start a process in debugging mode

Exec args:

-Xagentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=<port>

  • agentlib=jdwp - Load the jdwp agent, for debugging.
  • transport=dt_socket - for connecting a debugging client over the network.
  • server=y - this one is the server half of the debugging.
  • suspend=y - don’t start executing until a client debugger attaches.
  • address=7777 - port we listen on.

Jdb

  • jdb -attach : attach debug process start with debugging mode.