我刚刚读到这句话:
format()方法做的第一件事是从名为output.vm的类路径加载Velocity模板
请解释在这种情况下类路径是什么意思,以及我应该如何设置类路径。
我刚刚读到这句话:
format()方法做的第一件事是从名为output.vm的类路径加载Velocity模板
请解释在这种情况下类路径是什么意思,以及我应该如何设置类路径。
当前回答
此上下文中的类路径与一般上下文中的类路径完全相同:VM知道它可以在任何地方找到要加载的类和资源(例如输出)。在你的情况下是这样)。
我理解Velocity希望找到一个名为output的文件。Vm在“无包”的任何位置。这可以是一个JAR,普通文件夹,…应用程序类路径中任何位置的根。
其他回答
可以把它看作是Java对PATH环境变量的回答——操作系统在PATH上搜索ex, Java在类路径上搜索类和包。
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
类路径是系统的一个环境变量。此变量的设置用于向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
设置CLASSPATH系统变量
要显示当前的CLASSPATH变量,在Windows和UNIX (Bourne shell)中使用这些命令: Windows: C:\>设置CLASSPATH 在UNIX中:% echo $CLASSPATH
要删除CLASSPATH变量的当前内容,可以使用以下命令: Windows: C:\> set CLASSPATH= 在UNIX中:% unset CLASSPATH;出口类路径
要设置CLASSPATH变量,可以使用以下命令(例如): Windows: C:\> set CLASSPATH=C:\users\george\java\classes UNIX下:% CLASSPATH=/home/george/java/classes;出口类路径