简体   繁体   中英

-classpath option for javac and java

I'm confused with the role -classpath option plays in both a java program. java程序时扮演的角色-classpath选项感到困惑。 Please help me understand.

Because they are two separate operations and not necessarily the same paths.

The runtime dependencies are often more extensive than the compile time dependencies. For example, many programs will code to interfaces, which limits the compile time dependencies to those interfaces. At runtime, the VM must be able to resolve the implementations of those interfaces, which are not required until they are loaded at runtime.

它只是在两种情况下告诉javac和java在哪里找到程序编译和运行所需的依赖项

它完成两次的原因是您编译代码的环境可能与您运行代码的环境不同。

Java loads classes at runtime. For example, you could write a method that forces loading of class X, compile it, write class X, compile it, and then run them together. In addition, you typically refer to classes by a fully specified name, but could run the same program with different versions of that class (eg, a different version of the library). Thus, you need to tell Java where it could potentially find the classes that it needs to load.

As for compilation, to ensure type safety, you have to provide the Java compiler at least with the interfaces or base classes that you are referring to and making calls on, so that the compiler can at least ensure that the call would be legal. For that reason, you have to tell it where to find the jars containing them.

Here is an example. Let's say you want to use JMS (a messaging framework) in a core Java program. At compile time, you need to at least tell javac where to find the JMS interfaces. At runtime, you need to provide these interfaces, but you also need to provide the JAR with the actual implementation (eg, ActiveMQ).

In C++ I believe it is the case that linking happens around compile-time, to create an executable (I am not a C++ programmer so I'm not sure about that).

In Java, the linker step happens at runtime (see the JVM spec, " Loading, Linking and Initalizing "). From your question it sounds like you understand why the classpath needs to be specified at compile time (because you might reference classes from third-party JARs in your code), so I will just explain that when your program is being run, those classes are not loaded into the JVM until they are referenced. At this point, the JVM needs to know where to find their representation.

The compiler has to know where to look to satisfy compile-time dependencies. The VM has to know where to look to satisfy runtime dependencies.

At compile time, you need to tell javac where to find third-party and user-defined classes. At runtime, you also need to tell java where to find third-party and user-defined classes. In both cases, one way to change the class path is to use the JDK Tools' -classpath option . Checkout the Setting the Class Path technical note for more details.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM