我刚刚读到这句话:

format()方法做的第一件事是从名为output.vm的类路径加载Velocity模板

请解释在这种情况下类路径是什么意思,以及我应该如何设置类路径。


当前回答

可以把它看作是Java对PATH环境变量的回答——操作系统在PATH上搜索ex, Java在类路径上搜索类和包。

其他回答

对于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

此上下文中的类路径与一般上下文中的类路径完全相同:VM知道它可以在任何地方找到要加载的类和资源(例如输出)。在你的情况下是这样)。

我理解Velocity希望找到一个名为output的文件。Vm在“无包”的任何位置。这可以是一个JAR,普通文件夹,…应用程序类路径中任何位置的根。

类路径是Java虚拟机在Java程序中查找用户定义的类、包和资源的路径。

在这个上下文中,format()方法从这个路径加载模板文件。

可以把它看作是Java对PATH环境变量的回答——操作系统在PATH上搜索ex, Java在类路径上搜索类和包。

类的静态成员可以直接调用,而无需创建对象实例。 由于主方法是静态的,Java虚拟机可以调用它,而不需要创建包含主方法的类的任何实例,这是程序的起点。