什么是类路径,该如何设置?

本文翻译自:What is a classpath and how do I set it?

I was just reading this line: 我只是在读这行:

The first thing the format() method does is load a Velocity template from the classpath named output.vm format()方法要做的第一件事是从名为output.vm的类路径中加载Velocity模板

Please explain what was meant by classpath in this context, and how I should set the classpath. 请解释在这种情况下类路径的含义,以及如何设置类路径。


#1楼

参考:https://stackoom.com/question/A3R7/什么是类路径-该如何设置


#2楼

classpath and path are the evironment variables . classpath和path是环境变量。 usually , you have to put the jdk/bin to path so that u could use java compiler everywhere , classpath is the path of your .class files . 通常,您必须将jdk / bin放在路径中,以便您可以在任何地方使用Java编译器,classpath是.class文件的路径。 the classpath has a default path a period(.) which means the current directory. classpath的默认路径为period(。),表示当前目录。 but when u used the packages . 但是当你使用这些包装的时候。 u would either specify the full path of the .class file or put the .class file path in the classpath which would save a lots of works ! 您要么指定.class文件的完整路径,要么将.class文件路径放在classpath中,这样可以节省很多工作!


#3楼

The classpath in this context is exactly what it is in the general context: anywhere the VM knows it can find classes to be loaded, and resources as well (such as output.vm in your case). 在这种情况下,类路径与在一般情况下完全相同:VM知道的任何地方都可以找到要加载的类以及资源(例如您的情况下的output.vm)。

I'd understand Velocity expects to find a file named output.vm anywhere in "no package". 我知道Velocity希望在“无包”中的任何位置找到一个名为output.vm的文件。 This can be a JAR, regular folder, ... The root of any of the locations in the application's classpath. 这可以是一个JAR,常规文件夹,...应用程序的类路径中任何位置的根。


#4楼

The classpath is the path where the Java Virtual Machine look for user-defined classes, packages and resources in Java programs. classpath是Java虚拟机在Java程序中查找用户定义的类,程序包和资源的路径。

In this context, the format() method load a template file from this path. 在这种情况下, format()方法从该路径加载模板文件。


#5楼

将其视为Java对PATH环境变量的回答-操作系统在PATH上搜索EXE,Java在类路径上搜索类和包。


#6楼

When programming in Java, you make other classes available to the class you are writing by putting something like this at the top of your source file: 使用Java进行编程时,可以通过在源文件的顶部放置以下类似内容,使其他类对正在编写的类可用:

import org.javaguy.coolframework.MyClass;

Or sometimes you 'bulk import' stuff by saying: 或者有时您通过说“批量导入”内容:

import org.javaguy.coolframework.*;

So later in your program when you say: 因此,稍后在程序中您说:

MyClass mine = new MyClass();

The Java Virtual Machine will know where to find your compiled class. Java虚拟机将知道在哪里可以找到您的已编译类。

It would be impractical to have the VM look through every folder on your machine, so you have to provide the VM a list of places to look. 让VM浏览计算机上的每个文件夹是不切实际的,因此您必须向VM提供要查看的位置列表。 This is done by putting folder and jar files on your classpath. 这是通过将文件夹和jar文件放在类路径上来完成的。

Before we talk about how the classpath is set, let's talk about .class files, packages, and .jar files. 在讨论如何设置类路径之前,让我们先谈谈.class文件,包和.jar文件。

First, let's suppose that MyClass is something you built as part of your project, and it is in a directory in your project called output . 首先,让我们假设MyClass是您在项目中构建的东西,它位于项目中名为output的目录中。 The .class file would be at output/org/javaguy/coolframework/MyClass.class (along with every other file in that package). .class文件位于output/org/javaguy/coolframework/MyClass.class (以及该包中的所有其他文件)。 In order to get to that file, your path would simply need to contain the folder 'output', not the whole package structure, since your import statement provides all that information to the VM. 为了到达该文件,您的路径只需要包含文件夹“输出”,而不是整个包结构,因为您的import语句将所有这些信息提供给VM。

Now let's suppose that you bundle CoolFramework up into a .jar file, and put that CoolFramework.jar into a lib directory in your project. 现在,假设您将CoolFramework捆绑到一个.jar文件中,然后将该CoolFramework.jar放入您项目中的lib目录中。 You would now need to put lib/CoolFramework.jar into your classpath. 您现在需要将lib/CoolFramework.jar放入类路径中。 The VM will look inside the jar file for the org/javaguy/coolframework part, and find your class. VM将在jar文件中查找org/javaguy/coolframework部分,并找到您的类。

So, classpaths contain: 因此,类路径包含:

  • JAR files, and JAR文件,以及
  • Paths to the top of package hierarchies. 程序包层次结构顶部的路径。

How do you set your classpath? 您如何设置类路径?

The first way everyone seems to learn is with environment variables. 每个人似乎学习的第一种方法是使用环境变量。 On a unix machine, you can say something like: 在UNIX机器上,您可以说以下内容:

export CLASSPATH=/home/myaccount/myproject/lib/CoolFramework.jar:/home/myaccount/myproject/output/

On a Windows machine you have to go to your environment settings and either add or modify the value that is already there. 在Windows计算机上,您必须转到环境设置,然后添加或修改已经存在的值。

The second way is to use the -cp parameter when starting Java, like this: 第二种方法是在启动Java时使用-cp参数,如下所示:

java -cp "/home/myaccount/myproject/lib/CoolFramework.jar:/home/myaccount/myproject/output/"  MyMainClass

A variant of this is the third way which is often done with a .sh or .bat file that calculates the classpath and passes it to Java via the -cp parameter. 这是第三种方法的变体,通常使用.sh.bat文件完成,该文件计算类路径并将其通过-cp参数传递给Java。

There is a "gotcha" with all of the above. 以上所有情况都有一个“陷阱”。 On most systems (Linux, Mac OS, UNIX, etc) the colon character (':') is the classpath separator. 在大多数系统(Linux,Mac OS,UNIX等)上,冒号(':')是类路径分隔符。 In windowsm the separator is the semicolon (';') 在windowsm中,分隔符为分号(';')

So what's the best way to do it? 那么最好的方法是什么?

Setting stuff globally via environment variables is bad, generally for the same kinds of reasons that global variables are bad. 通过环境变量在全局范围内设置内容是不好的,通常出于与全局变量不好的原因相同的原因。 You change the CLASSPATH environment variable so one program works, and you end up breaking another program. 您更改了CLASSPATH环境变量,以便一个程序可以工作,并且最终破坏了另一个程序。

The -cp is the way to go. -cp是解决方法。 I generally make sure my CLASSPATH environment variable is an empty string where I develop, whenever possible, so that I avoid global classpath issues (some tools aren't happy when the global classpath is empty though - I know of two common, mega-thousand dollar licensed J2EE and Java servers that have this kind of issue with their command-line tools). 我通常会确保我的CLASSPATH环境变量尽可能在开发时在其中为空字符串,以便避免出现全局类路径问题(尽管全局类路径为空时某些工具并不满意-我知道有两个常见的百万美元许可的J2EE和Java服务器在其命令行工具中存在此类问题)。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值