什么是类路径以及如何设置它? [英] What is a classpath and how do I set it?

查看:28
本文介绍了什么是类路径以及如何设置它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚读到这一行:

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

The first thing the format() method does is load a Velocity template from the classpath named output.vm

请解释在这种情况下类路径的含义,以及我应该如何设置类路径.

Please explain what was meant by classpath in this context, and how I should set the classpath.

推荐答案

使用 Java 编程时,您可以通过在源文件的顶部放置如下内容来使其他类可用于您正在编写的类:

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:

import org.javaguy.coolframework.MyClass;

或者有时你会说:

import org.javaguy.coolframework.*;

所以稍后在你的程序中,当你说:

So later in your program when you say:

MyClass mine = new MyClass();

Java 虚拟机将知道在哪里可以找到已编译的类.

The Java Virtual Machine will know where to find your compiled class.

让虚拟机查看您机器上的每个文件夹是不切实际的,因此您必须向虚拟机提供要查看的位置列表.这是通过将文件夹和 jar 文件放在类路径中来完成的.

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. This is done by putting folder and jar files on your classpath.

在讲如何设置classpath之前,我们先讲一下.class文件、包和.jar文件.

Before we talk about how the classpath is set, let's talk about .class files, packages, and .jar files.

首先,让我们假设 MyClass 是您作为项目的一部分构建的,它位于项目中名为 output 的目录中..class 文件将位于 output/org/javaguy/coolframework/MyClass.class(以及该包中的所有其他文件).为了访问该文件,您的路径只需要包含文件夹输出",而不是整个包结构,因为您的导入语句向 VM 提供了所有这些信息.

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. The .class file would be at output/org/javaguy/coolframework/MyClass.class (along with every other file in that package). 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.

现在假设您将 CoolFramework 捆绑到一个 .jar 文件中,然后将该 CoolFramework.jar 放入项目中的 lib 目录中.您现在需要将 lib/CoolFramework.jar 放入您的类路径中.VM 将查看 org/javaguy/coolframework 部分的 jar 文件,并找到您的类.

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. You would now need to put lib/CoolFramework.jar into your classpath. The VM will look inside the jar file for the org/javaguy/coolframework part, and find your class.

因此,类路径包含:

  • JAR 文件,以及
  • 包层次结构顶部的路径.

您如何设置类路径?

似乎每个人都学习的第一种方法是使用环境变量.在 unix 机器上,你可以这样说:

The first way everyone seems to learn is with environment variables. On a unix machine, you can say something like:

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

在 Windows 计算机上,您必须转到环境设置并添加或修改已有的值.

On a Windows machine you have to go to your environment settings and either add or modify the value that is already there.

第二种方式是在启动Java时使用-cp参数,像这样:

The second way is to use the -cp parameter when starting Java, like this:

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

这是第三种方式,通常使用 .sh.bat 文件来计算类路径并通过 将其传递给 Java>-cp 参数.

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.

有一个陷阱"以上所有.在大多数系统(Linux、Mac OS、UNIX 等)上,冒号字符 (':') 是类路径分隔符.在 windowsm 中,分隔符是分号 (';')

There is a "gotcha" with all of the above. On most systems (Linux, Mac OS, UNIX, etc) the colon character (':') is the classpath separator. In windowsm the separator is the semicolon (';')

那么最好的方法是什么?

通过环境变量全局设置东西是不好的,通常与全局变量不好的原因相同.您更改了 CLASSPATH 环境变量,以便一个程序运行,但最终却破坏了另一个程序.

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.

-cp 是要走的路.我通常确保我的 CLASSPATH 环境变量是我开发的空字符串,只要有可能,这样我就可以避免全局类路径问题(尽管当全局类路径为空时,某些工具并不满意 - 我知道两种常见的、价值数以千计美元许可的 J2EE 和 Java 服务器,它们的命令行工具存在此类问题).

The -cp is the way to go. 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).

这篇关于什么是类路径以及如何设置它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆