简体   繁体   English

我在哪里将类放在CLASSPATH上?

[英]Where do I put classes on CLASSPATH?

I complied Ref.java into ref.class and put it in the directory as specified below. 我将Ref.java编译为ref.class并将其放在下面指定的目录中。


I'm having a devil of a time figuring out how to use CLASSPATH. 我花了很多时间弄清楚如何使用CLASSPATH。 If anyone can point out my (probably stupid) error, I would be most happy. 如果有人能指出我的(可能是愚蠢的)错误,我将非常高兴。

I have a class "Ref.java" as below: 我有一个类“ Ref.java”,如下所示:

package utility.myapp;  
public class Ref {  
static public void print(String s)  
{  
// do something here  
}  
}  

My CLASSPATH includes: 我的CLASSPATH包括:

C:\Eclipse\classes

I stored the class as file: 我将类存储为文件:

C:\Eclipse\classes\utility\myapp\Ref.class

From my main project I import as: 从我的主要项目中,我导入为:

import utility.myapp.*

in file "Main.java" below: 在下面的文件“ Main.java”中:

package com.reftest;
import android.app.Activity;
import android.os.Bundle;
import utility.myapp.*;
public class Main extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
}
}

Eclipse says: Eclipse说:

The import utility cannot be resolved

I have tried every variation on this theme I could think of, but still no joy. 我已经尝试过可以想到的这个主题的所有变体,但仍然没有喜悦。
Any help appreciated. 任何帮助表示赞赏。

BTW - I didn't omit or simplify anything: I tried to build this actual and majorly trivial project just to get the structure right. 顺便说一句-我没有遗漏或简化任何事情:我试图构建这个实际且主要是琐碎的项目,只是为了使结构正确。

This all sounds terribly wrong to me. 这听起来对我来说是完全错误的。

C:\Eclipse\classes

is simply wrong. 是完全错误的。 You should create a new project, and the place where your .class files end up ought to be relative to your project root. 您应该创建一个新项目,.class文件最终的位置应该相对于项目根目录。

You should be able to right click on your Eclipse project and set up directories where JAR files live, etc. Eclipse ought to tell you a default place where it'll put .class files when you compile your .java; 您应该能够右键单击Eclipse项目,并设置JAR文件所在的目录等。Eclipse应该告诉您在编译.java时将.class文件放入的默认位置; make sure it's relative to your project root. 确保它相对于您的项目根目录。

You'd better not have an environment variable CLASSPATH. 您最好不要使用环境变量CLASSPATH。 That's the wrong way to do it. 那是错误的方法。

You need to make sure that your compiled class files are on the CLASSPATH . 您需要确保已编译的类文件位于CLASSPATH

Try putting Ref.class into 尝试将Ref.class放入

C:\Eclipse\classes\utility\myapp\

Thanks to all who provided input. 感谢所有提供意见的人。 As a service to the community, I provide a consolidation of all I learned below. 作为对社区的服务,我将在下面学到的所有知识进行合并。

There are two cases where a CLASSPATH is used. 两种情况下使用CLASSPATH。
1. During the build/debug cycle (inside Eclipse) 1.在构建/调试周期中(在Eclipse内部)
2. Running the app standalone (outside Eclipse). 2.独立运行应用程序(Eclipse外部)。

For #2 above, the environment variable CLASSPATH is consulted. 对于上述#2,请查询环境变量CLASSPATH。 It is a semicolon-delimited list of directories as well-documented everywhere. 它是一个用分号分隔的目录列表,在任何地方都有详细记录。 Setting it does not interfere with Eclipse - it is ignored. 设置它与Eclipse的干扰-它被忽略。

For #1, the classpath is stored in file ".classpath" in the root of your project in the workspace. 对于#1,类路径存储在工作区中项目根目录中的“ .classpath”文件中。 To add an external directory for class search, add a line of the format: 要为类搜索添加外部目录,请添加以下格式的行:

<classpathentry kind="lib" path="yourclasspath"/>

My .classpath entry looks like this 我的.classpath条目看起来像这样

<classpathentry kind="lib" path="C:/Eclipse/classes"/>  

This can also be set in the Eclipse GUI by right-clicking on the project, selecting Properties->Java Build Path. 右键单击项目,然后选择Properties-> Java Build Path,也可以在Eclipse GUI中进行设置。 Then select Add External Class Folder and enter the directory where you want to store your classes. 然后选择添加外部类文件夹,然后输入要存储类的目录。

Hope this is useful to others. 希望这对其他人有用。

-John -约翰

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM