什么是类路径? [英] What is a classpath?

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

问题描述

我刚读这一行:


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

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

在这种情况下,我无法弄清楚classpath的含义。

And I couldn't figure out what was meant by classpath in this context.

推荐答案

使用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是您在项目中构建的,它位于项目的一个目录中,名为输出。 .class文件位于 output / org / javaguy / coolframework / MyClass.class (以及该包中的所有其他文件)。为了获取该文件,您的路径只需要包含文件夹'output',而不是整个包结构,因为您的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.

第二种方法是使用 -cp 启动Java时的参数,如下所示:

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

那么最好的方法是什么?

通过环境变量全局设置内容很糟糕,通常出于与全局变量相同的原因s很糟糕。您更改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天全站免登陆