导入Java类文本编辑器 [英] Importing java classes text editor

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

问题描述

最近,我开始使用文本编辑器(文本伴侣)而不是使用eclipse进行Java编程.我在文件夹A(/Users/kevincastro/Documents/Code/estructuras/clases)中有file1.java,在文件夹B(/Users/kevincastro/Documents/Code/estructuras/datastructures)中有file2.java.我想将file2.java导入到file1.java中,但是它没有字.我已经尝试过了:

Recently I started using a text editor (text mate) instead of eclipse to program in Java. I hava file1.java in a folder A(/Users/kevincastro/Documents/Code/estructuras/clases) and a file2.java in folder B(/Users/kevincastro/Documents/Code/estructuras/datastructures). I want to import file2.java to file1.java but it doesn't word. I've tried :

import Users.kevincastro.Documents.Code.estructuras.datastructures.*; import datastructures.*; import Documents.Code.estructuras.datastructures.*;

它们都不起作用.我收到此错误包Users.kevincastro.Documents.Code.estructuras.datastructures不存在"

none of them work. I get this error "package Users.kevincastro.Documents.Code.estructuras.datastructures does not exist"

有帮助吗?谢谢

推荐答案

这里似乎存在严重的误解.您似乎认为import语句实际上是导入文件"或类似的东西.它没有做任何事情. import语句仅存在,因此您不必在每次使用时都编写类的完全限定名称.采取以下代码:

It looks like there is a serious misunderstanding here. You seem to think that the import statement actually "import files" or something like that. It doesn't do anything of the sort. The import statement only exists so that you don't have to write the fully qualified name of a class every time you use it. Take the following code :

package a
import b.Bar

public class Foo {
    private Bar bar = new Bar();
    public static void main(String [] args) {
        System.out.println(bar);
    }
}

与写作完全相同:

package a

public class Foo {
    private b.Bar bar = new b.Bar();
    public static void main(String [] args) {
        System.out.println(bar);
    }
}

但是它并没有说明b.Bar所在文件的位置!因此,在文本编辑器或源代码中没有要导入的文件.您唯一了解的是b.Bar必须位于名为b的文件夹中,但该文件夹可以位于任何位置.

But it doesn't say much about the location of the file where b.Bar is located! So there is no file to import in your text editor or in your source code. The only thing you know is that b.Bar must be in a folder named b - but that folder can be anywhere.

  1. 编译a.Foo时,javac(编译器)必须要么同时编译b.Bar,要么可以访问类路径中的b.Bar.假设您的源代码位于/SomeDir/src/a/Foo.java和/SomeDir/src/b/Bar.java中,并且已编译为/SomeDir/target.例子:

  1. When you compile a.Foo, javac (the compiler) must either compile b.Bar at the same time, or have access to b.Bar in the classpath. Let's say that you sources are in /SomeDir/src/a/Foo.java and /SomeDir/src/b/Bar.java, and that you compile to /SomeDir/target. Examples :

  • 一次编译两个类(实际上,/SomeDir/src中的所有java源文件):javac -d /SomeDir/target /SomeDir/src/**/*.java

编译Foo,引用Bar(已经编译到/SomeOtherDir/b/Bar.class):javac -d /SomeDir/target -classpath /SomeOtherDir /SomeDir/src/a/Foo.java

Compile Foo, referencing Bar (already compiled to /SomeOtherDir/b/Bar.class): javac -d /SomeDir/target -classpath /SomeOtherDir /SomeDir/src/a/Foo.java

或者Bar.class可以在jar文件中,等等.

Or Bar.class could be in a jar file, etc.

运行Foo时,Bar必须在类路径中,以便类加载器可以找到它.如果要运行/SomeDir/a/Foo.class,而Bar在/SomeOtherDir/b/Bar.class中:java -classpath "/SomeDir:/SomeOtherDir" a.Foo

When you run Foo, Bar must be in the classpath, so that it can be found by the classloader. If you want to run /SomeDir/a/Foo.class, and Bar is in /SomeOtherDir/b/Bar.class: java -classpath "/SomeDir:/SomeOtherDir" a.Foo

测试了所有这些内容之后,就应该准备好爱上Maven或Gradle之类的现代构建工具,这将使您的生活更加轻松.

Once you have tested all that, you should be ready to fall in love with modern build tools such as Maven or Gradle, which will make your life much easier.

这篇关于导入Java类文本编辑器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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