Java Import Package (to package above present work directory)

十年热恋 提交于 2019-12-08 09:40:05

问题


how do I import package (to package above present work directory) in Java?

here is the directory structure:

Coba.java

import halo.*;

public class Coba
{
    public static void main(String args[])
    {
        Orang org = new Orang();
        System.out.println(org.a);
    }
}

Orang.java

package halo;
// I can't import kabar.*; since it's above present work directory

public class Orang
{
    public int a;

    public Orang()
    {
        this.a = 1;
    }

    public void haha()
    {
        /*
            i want to:
            Tes t = new Tes();
            System.out.println(t.b);
        */
    }
}

Tes.java

package kabar;

public class Tes
{
    public int b;

    public Tes()
    {
        this.b = 2;
    }
}

Question:

How do I access variable b in class Tes by importing class kabar.Tes from class Orang?

If i write

import kabar.Tes;

in class Orang. It doesn't work because class Orang is above present work directory.

Thank you very much.

BTW, I don't use Netbeans or Eclipse. I want to know the basic how it works, so I just use simple text editor.


回答1:


David, The location of the directories doesn't matter. It's the packages that matter. You can add multiple directories to your classpath when you compile/run the program to refer to these extra directories.




回答2:


You need to read some very basic tutorials. This particular topic is covered here, and other Sun tutorials might be useful as well.



来源:https://stackoverflow.com/questions/4200815/java-import-package-to-package-above-present-work-directory

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!