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

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

问题描述

我只是在读这一行:

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

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

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

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;

或者有时您通过说批量导入"内容

Or sometimes you 'bulk import' stuff by saying:

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.

让VM浏览计算机上的每个文件夹是不切实际的,因此您必须向VM提供要查看的位置列表.这是通过将文件夹和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.

在讨论如何设置类路径之前,先讨论.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(以及该软件包中的所有其他文件).为了获取该文件,您的路径只需要包含文件夹输出",而不是整个包结构,因为您的import语句会将所有这些信息提供给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将在jar文件中查找org/javaguy/coolframework部分,并找到您的类.

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文件完成,该文件计算类路径并将其通过-cp参数传递给Java.

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天全站免登陆