我刚刚读到这句话:
format()方法做的第一件事是从名为output.vm的类路径加载Velocity模板
请解释在这种情况下类路径是什么意思,以及我应该如何设置类路径。
我刚刚读到这句话:
format()方法做的第一件事是从名为output.vm的类路径加载Velocity模板
请解释在这种情况下类路径是什么意思,以及我应该如何设置类路径。
当前回答
类路径是Java世界中的基本概念之一,Java程序经常误解或根本不理解它,尤其是初学者。
简单地说,类路径只是一组路径,java编译器和JVM必须在其中找到所需的类来编译或执行其他类。
让我们从一个例子开始,假设我们有一个Main.java文件,位于C:\Users\HP\Desktop\org\example目录下,
package org.example;
public class Main {
public static void main(String[] args) {
System.out.println("Hello world");
}
}
现在,假设我们在C:\目录下,我们想编译我们的类,这很简单,只需要运行:
javac .\Users\HP\Desktop\org\example\Main.java
现在来回答比较难的问题,我们在同一个文件夹C:\中,我们想要运行编译后的类。
不管你想到的答案是什么,正确的答案是:
java -cp .\Users\HP\Desktop org.example.Main
我会解释为什么,首先,我们要ro的类的名字是org。example。Main而不是Main,或者Main。class或者。\users\hp\desktop\org\example\Main。class !这就是在包下声明的类的工作方式。
Now, we provided the name of the class to the JVM (java command in this case), But how it (JVM) will know where to find the .class file for the Main class? Thats where the classpath comes into picture. Using -cp flag (shortcut for -classpath), we tell the JVM that our Main.class file will be located at C:\users\hp\Desktop.. In fact, not really, we tell it to just go to the Desktop directory, and, because of the name of the class org.example.Main, the JVM is smart and it will go from Desktop to org directory, and from org to example directory, searching for Main.class file, and it will find it and it will kill it, I mean, it will run it :D .
现在让我们假设在Main类中,我们想要使用另一个名为org.apache.commons.lang3.StringUtils的类,后者位于名为commons-lang3-3.10.jar的jar文件中,该文件位于C:\Users\HP\Downloads. jar中Main.java现在看起来是这样的:
package org.example;
import org.apache.commons.lang3.StringUtils;
public class Main {
public static void main(String[] args) {
System.out.println("Hello world");
System.out.println(StringUtils.equals("java", "java")); //true
}
}
如何编译Main.java如果我们总是在C:\ ?答案是:
javac -cp .\Users\HP\Downloads\commons-lang3-3.10.jar .\Users\HP\Desktop\org\example\Main.java
.\Users\HP\Desktop\org\example\Main.java是因为文件系统中有。java文件。 -cp .\Users\HP\Downloads\commons-lang3-3.10.jar是因为java编译器(在本例中是javac)需要知道类org.apache.commons.lang3. stringutils的位置,所以我们提供了jar文件的路径,然后编译器将进入jar文件,并尝试在org\apache\commons\lang3目录中找到一个文件StringUtils.class。
如果我们想运行Main.class文件,我们将执行:
java -cp ".\Users\HP\Desktop\;.\Users\HP\Downloads\commons-lang3-3.10.jar" org.example.Main
main是类的名称。 ".\Users\HP\Desktop\;.\Users\HP\Downloads\commons-lang3-3.10.jar"为路径(由;在Windows中)到Main和StringUtils类。
其他回答
对于linux用户,为了总结和补充其他人在这里所说的,你应该知道以下内容:
$CLASSPATH is what Java uses to look through multiple directories to find all the different classes it needs for your script (unless you explicitly tell it otherwise with the -cp override). Using -cp requires that you keep track of all the directories manually and copy-paste that line every time you run the program (not preferable IMO). The colon (":") character separates the different directories. There is only one $CLASSPATH and it has all the directories in it. So, when you run "export CLASSPATH=...." you want to include the current value "$CLASSPATH" in order to append to it. For example: export CLASSPATH=. export CLASSPATH=$CLASSPATH:/usr/share/java/mysql-connector-java-5.1.12.jar In the first line above, you start CLASSPATH out with just a simple 'dot' which is the path to your current working directory. With that, whenever you run java it will look in the current working directory (the one you're in) for classes. In the second line above, $CLASSPATH grabs the value that you previously entered (.) and appends the path to a mysql dirver. Now, java will look for the driver AND for your classes. echo $CLASSPATH is super handy, and what it returns should read like a colon-separated list of all the directories, and .jar files, you want java looking in for the classes it needs. Tomcat does not use CLASSPATH. Read what to do about that here: https://tomcat.apache.org/tomcat-8.0-doc/class-loader-howto.html
可以把它看作是Java对PATH环境变量的回答——操作系统在PATH上搜索ex, Java在类路径上搜索类和包。
类的静态成员可以直接调用,而无需创建对象实例。 由于主方法是静态的,Java虚拟机可以调用它,而不需要创建包含主方法的类的任何实例,这是程序的起点。
类路径是Java虚拟机在Java程序中查找用户定义的类、包和资源的路径。
在这个上下文中,format()方法从这个路径加载模板文件。
CLASSPATH是Java编译器和运行时定位Java程序中使用的Java包所需的环境变量(即对所有进程可用的操作系统全局变量)。(为什么不调用PACKAGEPATH?)这类似于另一个环境变量PATH,它被CMD shell用来查找可执行程序。
CLASSPATH可以通过以下方式之一设置:
CLASSPATH can be set permanently in the environment: In Windows, choose control panel ⇒ System ⇒ Advanced ⇒ Environment Variables ⇒ choose "System Variables" (for all the users) or "User Variables" (only the currently login user) ⇒ choose "Edit" (if CLASSPATH already exists) or "New" ⇒ Enter "CLASSPATH" as the variable name ⇒ Enter the required directories and JAR files (separated by semicolons) as the value (e.g., ".;c:\javaproject\classes;d:\tomcat\lib\servlet-api.jar"). Take note that you need to include the current working directory (denoted by '.') in the CLASSPATH.
To check the current setting of the CLASSPATH, issue the following command:
> SET CLASSPATH
CLASSPATH can be set temporarily for that particular CMD shell session by issuing the following command:
> SET CLASSPATH=.;c:\javaproject\classes;d:\tomcat\lib\servlet-api.jar
Instead of using the CLASSPATH environment variable, you can also use the command-line option -classpath or -cp of the javac and java commands, for example,
> java –classpath c:\javaproject\classes com.abc.project1.subproject2.MyClass3