Java 17 Recipes
Download 3.2 Mb. Pdf ko'rish
|
Java 17 Recipes
How It Works
Java implements the concept of a classpath. This is a directory search path that you can specify system-wide using the CLASSPATH environment variable. You can also specify the classpath for a specific invocation of the JVM via the java command’s -classpath option. When executing Java programs, the JVM finds and loads classes as needed using the following search order. 1. The classes that are fundamental to the Java platform and are contained in the Java installation directory 2. Any packages or JAR files that are located within the extension directory of the JDK 3. Packages, classes, JAR files, and libraries that are loaded somewhere on the specified classpath You may need to access more than one directory or JAR file for an application. This could be the case if your dependencies are located in more than one location. To do so, simply use the delimiter for your operating system (a ; for Windows and a : for Unix OS) as a separator between the locations specified by the CLASSPATH variable. The following is an example of specifying multiple JAR files in the CLASSPATH environment variable on Unix and Linux systems. export CLASSPATH=/JAVA_DEV/some-jar.jar:/JAVA_LIB/myjar.jar Alternatively, you can specify the classpath via a command-line option. javac –classpath /JAVA_DEV/some-jar.jar:/JAVA_LIB/myjar.jar... When loading the resources for a Java application, the JVM loads all the classes and packages specified in the first location, followed by the second, and so on. This is important because the order of loading may make a difference in some instances to avoid interfering with one another. Note JAR files package applications and Java libraries into a distributable format. If you have not packaged your application in that manner, you may simply specify the directory or directories in which your .class files reside. ChApteR 1 GettInG StARted wIth JAvA 17 25 Sometimes you want to include all JAR files within a specified directory. Do that by specifying the wildcard character (*) after the directory containing the files. The following is an example. javac –classpath /JAVA_DEV/*:/JAVA_LIB/myjar.jar... Specifying a wildcard tells the JVM that it should only load JAR files. It does not load class files located in a directory specified with the wildcard character. You need to specify a separate path entry for the same directory if you also want the class files. The following is an example. javac –classpath /JAVA_DEV/*:/JAVA_DEV Subdirectories within the classpath are not searched. To load files contained within subdirectories, those subdirectories and/or files must be explicitly listed in the classpath. However, Java packages that are equivalent to the subdirectory structure are loaded. Therefore, any Java classes that reside within a Java package that is equivalent to the subdirectory structure are loaded. Note It is a good idea to organize your code; it is also good to organize where you place your code on the computer. A good practice is to place all your Java projects within the same directory; it can become your workspace. place all the Java libraries that are contained in JAR files into the same directory for easier management. You can also use tools like Maven and Gradle to organize your project. Download 3.2 Mb. Do'stlaringiz bilan baham: |
Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling
ma'muriyatiga murojaat qiling