开发者

Where do I put classes on CLASSPATH?

开发者 https://www.devze.com 2023-04-07 16:48 出处:网络
I complied Ref.java into ref.class and put it in the directory as specified below. I\'m having a devil of a time figuring out how to use CLASSPATH.If anyone can point out my (probably stupid) error

I complied Ref.java into ref.class and put it in the directory as specified below.


I'm having a devil of a time figuring out how to use CLASSPATH. If anyone can point out my (probably stupid) error, I would be most happy.

I have a class "Ref.java" as below:

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

My CLASSPATH includes:

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:

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:

The import utility cannot be resolved

I have tried every variation on this theme I could think o开发者_C百科f, 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.

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; make sure it's relative to your project root.

You'd better not have an environment variable CLASSPATH. That's the wrong way to do it.


You need to make sure that your compiled class files are on the CLASSPATH.

Try putting Ref.class into

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.
1. During the build/debug cycle (inside Eclipse)
2. Running the app standalone (outside Eclipse).

For #2 above, the environment variable CLASSPATH is consulted. It is a semicolon-delimited list of directories as well-documented everywhere. Setting it does not interfere with Eclipse - it is ignored.

For #1, the classpath is stored in file ".classpath" in the root of your project in the workspace. 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

<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. Then select Add External Class Folder and enter the directory where you want to store your classes.

Hope this is useful to others.

-John

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号