Notes on Java path and Package

① 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

Notes on Java path and Package
Upload and download notes in java on S3
[Java] Basic types and instruction notes
java notes
Notes on signal control in Java
Notes on Android (java) thread processing
Install Java 9 on windows 10 and CentOS 7
Notes on operators using Java ~ String type ~
Notes on expand () and collapse () of Expandablerecyclerview
Notes on Java's Stream API and SQL
[Java] Array notes
JAVA_HOME and Path
Java and JavaScript
XXE and Java
Java serialization notes
Install java and android-sdk on Mac using homebrew
Put Java 11 and spring tool suite on mac
Java management and STS (Eclipse) build path settings
Java upload and download notes to Azure storage
Compile and run Java on the command line
Notes on implementing google books api in java okhttp and gson versions and okhttp and jackson versions
Let's touch on Java
Notes on Protocol Buffers
[Java] Stream Collectors notes
Java formatted output [Notes]
Getters and setters (Java)
Install Java on Mac
[Java] Thread and Runnable
[Java] Package for management
Java true and false
python notes on docker
[Android] Notes on xml
[Java] String comparison and && and ||
Run PostgreSQL on Java
[Java] Control syntax notes
credentials.yml.enc and master.key <Notes>
Notes on multiple inheritance
Install java and maven using brew on new mac
Notes on regular expressions
Java --Serialization and Deserialization
[Java] Arguments and parameters
[Java] Branch and repeat
Modules and Mixins notes
[Java] Variables and types
java (classes and instances)
[Java] Development with multiple files using package and import
Notes on how to use regular expressions in Java
[Java] Overload and override
Generate and execute Jar file of Java file belonging to package
How to write and notes when migrating from VB to JAVA
Study Java # 2 (\ mark and operator)
Java version control on macOS
Java version 8 and later features
Install OpenJDK7 (JAVA) on ubuntu 14.04
Downgrade Java on openSUSE Linux
[Java] Difference between == and equals
[Java] Stack area and static area
Try scraping using java [Notes]
Reflection on Java string manipulation
[Java] Generics classes and generics methods
Java programming (variables and data)