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.
・ CentOS7.4 · Java 1.8.0_171 ・ Vi 7.4 ・ Tree 1.6.0
Create a directory to verify the operation.
# cd
# mkdir -p JAVA/src
# cd JAVA
# mkdir class
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.
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
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.
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.
Click here for the execution result.
TestJava2 GO! i =1 c = A GO! i =2 c = B
that's all.
Recommended Posts