Java类路径问题 [英] Java Classpath Issue

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

问题描述

我有两个类:


MyApplication

MyApplication
Library

该库已被编译为Library.class,并且源代码不再可用。我现在正在尝试从源代码编译MyApplication。 MyApplication取决于库。该库的软件包名称为org.myCompany。我尝试将类路径设置为以下内容:

The Library has already been compiled into Library.class and the source code is no longer available. I am now trying to compile MyApplication from source. MyApplication depends on the Library. The Library has a package name of org.myCompany. I tried setting my classpath to the following:

set CLASSPATH=C:\java\project\org\myCompany\Library.class;.

这会产生以下javac编译器错误消息:

which produced the following javac compiler error message:

MyApplication.java:33: cannot find symbol
symbol: class Library
location: class MyApplication
          Library theLibrary = new Library();

所以我将类路径更改为:

So I changed my classpath to be:

set CLASSPATH=C:\java\project\;.

会产生完全相同的错误消息。

which produced the exact same error message.

如何设置Windows类路径以包含Library.class文件?是否应该指向包含org\myCompany子文件夹的文件夹?还是直接指向类文件?还是到包含类文件的文件夹中(即使该类在一个包中并且属于一个子文件夹)?

How do I set my Windows classpath to include the Library.class file? Should it point at the folder contains the org\myCompany subfolders? Or point directly to the class file? Or to the folder containing the class file (even though the class is in a package and belongs in a subfolder)?

我在执行set命令后执行echo%CLASSPATH%并且类路径已正确设置。我还制作了一个ant build.xml文件,并遇到了同样的问题。实际上,ant -verbose确认我的类路径设置正确。

I do an echo %CLASSPATH% after my set command and the classpath is being set correctly. I also made an ant build.xml file and encountered the same problem. In fact, ant -verbose confirmed that my classpath is being set correctly.

推荐答案

您不能像在类路径中添加单个类这个。您有3种解决方案:

You cannot add a single class in your classpath like this. You have 3 solutions:


  • 将此类添加到其他已编译类的路径中(尊重目录的包命名)

  • 在您的类路径中添加此类的根目录(在您的情况下为 C:\java\project\)

  • 添加该类放入罐子中,然后将此罐子添加到类路径中

对于您的问题,第三个选择更干净:通常将外部依赖项打包到jar中文件。

For your problem, the thrird choice is cleaner: external dependencies normally are packaged into jar files.

这篇关于Java类路径问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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