① Premise Configuration directory
Executable and called file 実行ファイルはBondTest.java 実行ファイルから呼び出すファイルはPosition.javaとBond.java
② Regarding the package name For the package name, give one folder name that contains the source file.
BondTest.java package test10; Position.java package issues; Bond.java package issues;
③ Classpath It was important to attach the root directory of the package. Since BondTest.java is package test10 this time, it is necessary to specify stage11. Also, since the file to be called is package issues, it is necessary to specify test10, which is one level higher. There are the above two classpaths.
④import The import statement is required when calling Position.java and Bond.java from BondTest.java. Since the package of BondTest.java is test10, the relative path is issues. ~. Therefore
BondTest.java import issues.Position; import issues.Bond;
⑤ javac and java It's finally time to compile with javac and run it with java. javac -cp ..:. BondTest.java Is the correct answer. .. is the root directory of the package of the executable file. . Is the root directory of package issues in Position.java and Bond.java. The file name is BondTest.java, which is surprisingly wrong and I want to do something like test10.BondTest.java, but I don't write it.
Next, execute it with the java command. -cp remains the same, so it's almost the same, but there is one important point. javac -cp ..:. test10.BondTest.java Is the correct answer. Note that it is easy to forget to write from the package name in the java command.
To summarize at the end
All directories are available Be in the executable directory Under this premise Specify the root directory of the package name in the classpath The package name should not be given in javac but in java
This is the point. It is important to search to find a directory that suits your shape, as people with different directory structures may not work.
Recommended Posts