A world where you can release a product without knowing the javac command

Trigger

――It's been my second year as a new graduate and working as a system engineer, but this time the Java EE web application system I was involved in will be released for commercial use.

――When releasing the system, there are talks such as "Heap's old area is ~" and "Metaspace usage rate is ~" in performance tests. So I wanted to reconfirm "How does Java work?", And was introduced when I participated in JJUG CCC 2018 Spring [Java 100 knock](https://www.slideshare. I decided to try net / JSUX Design / java100).

About Java 100 knock

――This is a collection of Java problems created by Justsystem. It is targeted at beginners to intermediate users, and it is said that it was created with the aim of producing more code that can operate stably in an operating environment and has excellent maintainability and expandability. ――I wanted to repeatedly establish these basics in order to improve my application skills, so I tried to solve the first one, but ... I realized that I didn't understand Java at all more than I expected. It's gone. Below is a memo when I solved the first one.

1st

--Test is implemented by shell script ... -Refer to this site --The JDK is already installed, so I will omit it. --Create Answer.java under src (do not use IDE such as eclipse!)

--Compile ... What are the javac options?

--Official oracle documentation

--Specify the classpath - -cp ./src --Encoding specification - -encoding --There is a "command line argument file" that specifies one or more files that contain arguments to the javac command (excluding the -J option) in order to shorten or simplify the javac command line. I can. You can use this method to create javac commands of any length on any operating system.

compile

javac -cp ./src -encoding UTF-8 ./src/Answer001.java

Run

--The java command option is here --After launching the Java Runtime Environment, load the specified class and call the ** main ** method of this class to launch the Java application.

java -cp src/ Answer001

--- cp → classpath If this is not specified, the specified location of the class file will be the current directory.

Fix

--Is it customary to create a classes directory for class files? Therefore, I will correct it.

javac -cp ./src -d ./classes src/Answer001.java
java -cp classes/ Answer001

The -d option of --javac does not create a directory, so you need to create one.

Test run

--While looking at test.sh

--It seems that junit.jar and hamcrest.jar are required under content / lib, so download and get them. -From here

--If the combination of junit.jar and hamcrest-core.jar versions doesn't work

Occurs

--The combination of junit 4.12 & hamcrest 1.3 worked fine

Take a look at the bytecode

--There were times when I read Java's iron rules and sometimes considered the good and bad of code from bytecode. Let's take a look at the bytecode. The most popular example would be string addition for String and StringBuffer

--Use javap command, Documents here ――It says "Disassemble the class file." --You can see the bytecode instructions by using the -c option. -"Displays the code that is disassembled for each method of the class, that is, the instructions that consist of Java bytecode. These are the" Java Virtual Machine Specification "(http://java.sun.com/docs/). It is documented in books / vmspec /). " ――It's a huge number of pages ...

javap -c answer/classes/Answer001.class > Answer001-bytecode.txt
Compiled from "Answer001.java"
public class Answer001 {
  public Answer001();
    Code:
       0: aload_0
       1: invokespecial #1                  // Method java/lang/Object."<init>":()V
       4: return

  public static void main(java.lang.String[]);
    Code:
       0: getstatic     #2                  // Field java/lang/System.out:Ljava/io/PrintStream;
       3: ldc           #3                  // String Hello, World!
       5: invokevirtual #4                  // Method java/io/PrintStream.println:(Ljava/lang/String;)V
       8: return
}

--Successfully generated the above bytecode --invoke special… Call a method. special handling for superclass, private, and instance initialization method invocations --Calling the default constructor --ldc ... Push item from run-time constant pool (push "Hello, World!" To the runtime constant pool) --invoke virtual ... calls a method on the instance. First stack the instance itself on the stack, then stack the arguments in sequence, then call it with INVOKE VIRTUAL. - http://jou4.hateblo.jp/entry/2015/10/17/103250 --In short, I interpreted "Hello, World!" On the stack as the argument of the println method with invoke virtual.

Impressions and considerations

――As you can see from the memo, you can see that you are solving while desperately checking javac, java, classpath, etc. (It's really embarrassing to expose) ――But I am a person who is about to release Java applications to the world. I felt that such a person would not even be able to compile and execute Java ... in fact, it is in a great world. IDE, the power of frameworks, terrifying. .. .. ――The moment I thought that what is happening in the world of food where you can eat warm and delicious rice if you rent it without knowing how to cut and season the ingredients is also happening in system development. was. ――But I am a system engineer. In the world of food, he is a chef. It makes me think that the fact is that the lunch box I bought at a convenience store is served with lentin, even though I call myself a chef. At least I don't want to eat that kind of food. ――In the world of cooking, a system I made so that the chef can respond by asking the chef, "How do you cook this ingredient?" "I want you to change this taste a little." I wanted to be able to answer to the people who used the game, "How does this work?" And "I want you to change the behavior here a little." ――Recently, I feel that the number of study sessions with titles such as "I'll try to move difficult things because it's okay" or "Beginners can use web applications in one day" is increasing. I think this is a manifestation of the fact that the threshold for computer science is falling. According to a 15-year warrior engineer, the IDE itself was very expensive in the first place, and it seems that it was not possible to develop a decent system without being in a company. ――However, no matter how free and easy it is to create a system, the underlying technology is not easy. It's just that very smart people hide the difficult parts to make them easier to use. However, if you keep up with the favors of those people, you will become a person who can do nothing when you say "There is a problem with performance". As a person who delivers the system, I wanted to understand the mechanism of the underlying part and deliver a high-quality system.

Recommended Posts

A world where you can release a product without knowing the javac command
Site where you can see the version relation of spring (?)
It's a picture diary of summer vacation where you can see the unspecified sequence of the human genome.