[CENTOS] By checking the operation of Java on linux, I was able to understand compilation and hierarchical understanding.

The other day, I learned about Java compilation and package management at an in-house study session. Since I could only edit what the environment was created with eclipse, setting compile options and creating a package gave me an opportunity to better understand the behavior of compilation and how to manage packages. I will output it here as a review of myself.

environment

・ CentOS7.4 · Java 1.8.0_171 ・ Vi 7.4 ・ Tree 1.6.0

Preparation

Create a directory to verify the operation.

# cd
# mkdir -p JAVA/src
# cd JAVA
# mkdir class

Compile & run

If you look at the directory hierarchy from the current directory with the tree command, it will be as follows.

# tree --charset=c
.
|-- class
`-- src

First, move to src and create a test source file.

# cd src
# vi TestJava.java

TestJava.java



class TestJava {

    public static void main(String[] args){

        System.out.println("test");

    }

}

After creating with the above contents, if you check the directory hierarchy, it will be as follows.

# tree ../ --charset=c
../
|-- class
`-- src
    `-- TestJava.java

Compile the created file. Here, if you compile without specifying options, a class file will be created under src, but it is troublesome to move the class file to another directory after creating it. Even in eclipse, this area was done automatically, so I did not feel it, but if you do it manually, specify the option so that it will be created in the specified directory (class directory) at the time of creation. Use the "-d" option to specify the directory.

# javac -d ../class TestJava.java

After execution, check the status of the directory hierarchy.

# tree ../ --charset=c
../
|-- class
|   `-- TestJava.class
`-- src
    `-- TestJava.java

The file is created in the specified class directory. In this way, the source file and class file are managed separately so that no one can edit the source without permission.

To execute this from other than the class directory at runtime, specify the file path with the "-cp" option, and it can be executed even if there is no file in the current directory. This is the method when CLASSPATH set by environment variable is performed on the command line.

# java -cp ../class TestJava
test

It was executed and "test" was output.

package

If the number of class files increases, manage the packages separately to make it easier to manage. Also, it is good to avoid name conflict even if a file with the same name happens to be created when developing with multiple people. First, create a directory for the package in the src directory, delete the class file once, and verify.

# mkdir pack1
# mv TestJava.java pack1
# rm -rf ../class/TestJava.class
# cd pack1
# tree ../../ --charset=c
../../
|-- class
`-- src
    `-- pack1
        `-- TestJava.java

Only in the src directory, there is a file created in the package directory.

When using the package, add the following description at the beginning of the source.

package package name

A description example is as follows.

TestJava.java


package pack1;

class TestJava {

    public static void main(String[] args){

        System.out.println("test");

    }

}

Describe the created package name at the beginning of the source file like this. This time, we will only test with a simple package name, but in reality, we will create a directory by digging a hierarchy like jp / co / testcomp / cv. Then, when describing it in the source, the hierarchy delimiter is represented by "." Like `package jp.co.testcomp.cv;`. As in the example, it is recommended that the package name start with the reverse of the Internet domain name. This is because it is recommended to name the package from the domain name that differs depending on the country or company so that the package name does not conflict when using programs of other companies or other countries.

Let's try again.

# javac -d ../class pack1/TestJava.java
# tree ../ --charset=c
../
|-- class
|   `-- pack1
|       `-- TestJava.class
`-- src
    `-- pack1
        `-- TestJava.java

A file was created for each pack1 directory in the class directory. If there is no package description in the source file, only the class file will be created without the pack1 folder in the class directory.

It is good to match the hierarchy of source and class files so as not to cause confusion in the storage destination. Therefore, do not forget the description of the package. (Although eclipse can detect errors)

By the way, the execution is specified as follows.

# java -cp ../class pack1.TestJava
test

In the classpath, specify the class directory, but from the package, specify it as `` `pack1.TestJava```. It cannot be executed by the specified method like the failure example.

Failure example)

# java -cp ../class/pack1 TestJava
# java -cp  ../class TestJava
# java  ../class/pack1/TestJava

Compile & execute multiple classes

First, I modified the file as follows.

TestJava.java


package pack1;

class TestJava {
    int i;
    char c;

    TestJava(){
        i = 1;
        c = 'A';
    }

    TestJava( int p_i, char p_c){
        i = p_i;
        c = p_c;
    }

    void go() {
        System.out.println("GO!");
        System.out.println("i =" + i + "\n" + "c = " + c );
    }

    public static void main(String[] args){

        System.out.println("test2");
        TestJava tj = new TestJava();
        TestJava tj_2 = new TestJava( 2, 'B');
        tj.go();
        tj_2.go();
    }

}

Create your own instance and output the one prepared with 2 patterns in the constructor. In addition, it is a test program that outputs even its own method.

I created another file.

TestJava2.java


package pack1;

class TestJava2 {

    public static void main(String[] args){
        System.out.println("TestJava2");
        TestJava tj2 = new TestJava();
        TestJava tj2_2 = new TestJava2( 2, 'B');
        tj2.go();
        tj2_2.go();
    }

}

This is the content to create an instance from the TestJava class and execute it.

Delete the class file that was created once and make it as follows.

# tree ../../ --charset=c
../../
|-- class
|   `-- pack1
`-- src
    `-- pack1
        |-- TestJava.java
        `-- TestJava2.java

There are the following ways to compile these two files.

    1. Compile multiple files at the same time.

javac -d ../../class TestJava.java TestJava2.java



 2. Compile the calling file and then compile the calling file
 In this case, TestJava2.java uses TestJava.java, so you need to specify the classpath as well.

javac -d ../../class TestJava.java

javac -d ../../class -cp ../../class TestJava2.java


 Click here for the execution result.

java -cp ../../class pack1.TestJava2

TestJava2 GO! i =1 c = A GO! i =2 c = B



 that's all.


Recommended Posts

By checking the operation of Java on linux, I was able to understand compilation and hierarchical understanding.
I didn't understand the behavior of Java Scanner and .nextLine ().
I tried to summarize the basics of kotlin and java
Recommendation of set operation by Java (and understanding of equals and hashCode)
I tried to summarize the methods of Java String and StringBuilder
I translated the grammar of R and Java [Updated from time to time]
[RxSwift] I want to deepen my understanding by following the definition of Observable
I compared the characteristics of Java and .NET
I touched on the new features of Java 15
I was swallowed by the darkness of the romaji, trying to convert my name to romaji
The CSV file that I was able to download suddenly started to appear on the page.
The part I was addicted to in "Introduction to Ajax in Java Web Applications" of NetBeans
I was addicted to the record of the associated model
Java classes and instances to understand in the figure
I summarized the types and basics of Java exceptions
20190803_Java & k8s on Azure The story of going to the festival
Command to check the number and status of Java threads
I was addicted to looping the Update statement on MyBatis
Understand the Singleton pattern by comparing Java and JavaScript code
I was addicted to the setting of laradock + VSCode + xdebug
Understand the Iterator pattern by comparing Java and JavaScript code
Reintroduction to Java for Humanities 0: Understanding the Act of Programming
[Java] I thought about the merits and uses of "interface"
I want to judge the necessity of testing by comparing the difference of class files when refactoring Java
[Scala] [Java] I tried to fix the directory traversal vulnerability when unzipping Zip4j by bytecode operation.
[Java] Various summaries attached to the heads of classes and members
I want to understand the flow of Spring processing request parameters
I want to return to the previous screen with kotlin and java!
I tried to deepen my understanding of object orientation by n%
I want to limit the input by narrowing the range of numbers
I tried to check the operation of gRPC server with grpcurl
From fledgling Java (3 years) to Node.js (4 years). And the impression of returning to Java
[Java] I tried to make a maze by the digging method ♪
I examined the concept of the process to understand how Docker works
I tried to display the calendar on the Eclipse console using Java.
I was addicted to the API version min23 setting of registerTorchCallback
I was able to deploy the Docker + laravel + MySQL app to Heroku!
A story that I was addicted to twice with the automatic startup setting of Tomcat 8 on CentOS 8
[java8] To understand the Stream API
The story of forgetting to close a file in Java and failing
I want to display images with REST Controller of Java and Spring!
I was addicted to a simple test of Jedis (Java-> Redis library)
Recorded because I was addicted to the standard input of the Scanner class
I tried to summarize the key points of gRPC design and development
[Ruby] I want to extract only the value of the hash and only the key
I want to pass the argument of Annotation and the argument of the calling method to aspect
[CircleCI] I was addicted to the automatic test of CircleCI (rails + mysql) [Memo]
[Java improvement case] How to reach the limit of self-study and beyond
Check the domain by checking the MX record of the email address with java
I want to get the field name of the [Java] field. (Old tale tone)
[Rails] The cause of not being able to post was in form_with
[Rails] Articles for beginners to organize and understand the flow of form_with
I tried to measure and compare the speed of GraalVM with JMH