java中的包帮助 - 导入不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/631682/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 17:09:35  来源:igfitidea点击:

Help with packages in java - import does not work

javaimportpackages

提问by Tim

I'm a C++ developer - not a java developer, but have to get this code working...

我是 C++ 开发人员 - 不是 Java 开发人员,但必须让这段代码工作......

I have 2 public classes that will be used by another product. I used the package directive in each of the java files.

我有 2 个公共类将被另一个产品使用。我在每个 java 文件中都使用了 package 指令。

package com.company.thing;

class MyClass ...

When I try to compile a test app that uses that I add

当我尝试编译使用我添加的测试应用程序时

import com.company.thing.*;

The javac compiler fails with errors about com.company does not exist. (even if I compile it in the same directory as the class files I just made a package of)

javac 编译器失败,出现关于 com.company 不存在的错误。(即使我将它编译在与我刚刚打包的类文件相同的目录中)

I am sure I am doing something bone-headed and silly.

我确信我在做一些愚蠢而愚蠢的事情。

I've read the http://java.sun.com/docs/books/tutorial/java/package/usepkgs.htmlpages and tried to set up a directory structure like /com/company/thing etc, but either I have totally screwed it all up or am missing something else.

我已经阅读了http://java.sun.com/docs/books/tutorial/java/package/usepkgs.html页面并尝试设置目录结构,如 /com/company/thing 等,但要么我有完全搞砸了,或者我错过了别的东西。

EDIT thanks for the suggestions - I had tried the classpath previously. It does not help.

编辑感谢您的建议 - 我之前尝试过类路径。它没有帮助。

I tried compiling

我试过编译

javac -classpath <parent> client.java 

and the result is:

结果是:

package com.company does not exist

I have the code I want to import (the two java files) in \com\company\product. I compile those fine. (they contain MyClass) I even made a jar file for them. I copied the jar file up to the parent directory.

我在 \com\company\product 中有要导入的代码(两个 java 文件)。我编译的很好。(它们包含 MyClass)我什至为它们制作了一个 jar 文件。我将 jar 文件复制到父目录。

I then did (in the parent directory with the client java file)

然后我做了(在带有客户端java文件的父目录中)

javac -cp <jarfile> *.java

the result is:

结果是:

cannot access MyClass
bad class file: MyClass.class(:MyClass.class)
class file contains wrong class: com.company.product.MyClass
Please remove or make sure it appears in the correct subdirectory of the classpath.

EDIT

编辑

I got the client code to compile and run if I used the fully qualified name for MyClass and compiled it in the parent directory. I am totally confused now.

如果我使用 MyClass 的完全限定名称并在父目录中编译它,我将编译和运行客户端代码。我现在完全糊涂了。

compiled with no classpath set - just

在没有设置类路径的情况下编译 - 只是

javac *.java 

in the parent directory - and it worked fine.

在父目录中 - 它工作正常。

I can get a test app to compile, but that is not going to cut it when i have to integrate it into the production code. Still looking for help.

我可以编译一个测试应用程序,但是当我必须将它集成到生产代码中时,这不会削减它。还在寻求帮助。

EDIT:

编辑:

Finally - not sure why it didn't work before - but I cleaned up all the files all over the directory structure and now it works.

最后 - 不知道为什么它以前不起作用 - 但我清理了整个目录结构中的所有文件,现在它可以工作了。

Thanks

谢谢

回答by erickson

It sounds like you are on the right track with your directory structure. When you compile the dependent code, specify the -classpathargument of javac. Use the parentdirectory of the comdirectory, where com, in turn, contains company/thing/YourClass.class

听起来您的目录结构是正确的。编译依赖代码时,指定 的-classpath参数javac。使用目录的目录com,其中com,依次包含company/thing/YourClass.class

So, when you do this:

所以,当你这样做时:

javac -classpath <parent> client.java

The <parent>should be referring to the parent of com. If you are in com, it would be ../.

<parent>应该是指的父com。如果你在com,那就是../

回答by duffymo

Yes, this is a classpath issue. You need to tell the compiler and runtime that the directory where your .class files live is part of the CLASSPATH. The directory that you need to add is the parent of the "com" directory at the start of your package structure.

是的,这是一个类路径问题。您需要告诉编译器和运行时您的 .class 文件所在的目录是 CLASSPATH 的一部分。您需要添加的目录是包结构开头的“com”目录的父目录。

You do this using the -classpath argument for both javac.exe and java.exe.

您可以使用 javac.exe 和 java.exe 的 -classpath 参数来执行此操作。

Should also ask how the 3rd party classes you're using are packaged. If they're in a JAR, and I'd recommend that you have them in one, you add the .jar file to the classpath:

还应该询问您使用的第 3 方类是如何打包的。如果它们在 JAR 中,并且我建议您将它们合二为一,则将 .jar 文件添加到类路径:

java -classpath .;company.jar foo.bar.baz.YourClass

Google for "Java classpath". It'll find links like this.

谷歌搜索“Java 类路径”。它会找到喜欢的链接

One more thing: "import" isn't loading classes. All it does it save you typing. When you include an import statement, you don't have to use the fully-resolved class name in your code - you can type "Foo" instead of "com.company.thing.Foo". That's all it's doing.

还有一件事:“导入”不是加载类。它所做的一切都可以节省您的打字时间。当您包含 import 语句时,您不必在代码中使用完全解析的类名 - 您可以键入“Foo”而不是“com.company.thing.Foo”。这就是它所做的一切。

回答by Bill K

You got a bunch of good answers, so I'll just throw out a suggestion. If you are going to be working on this project for more than 2 days, download eclipse or netbeans and build your project in there.

你得到了很多很好的答案,所以我只是抛出一个建议。如果您要在此项目上工作超过 2 天,请下载 eclipse 或 netbeans 并在其中构建您的项目。

If you are not normally a java programmer, then the help it will give you will be invaluable.

如果您通常不是 Java 程序员,那么它给您的帮助将是无价的。

It's not worth the 1/2 hour download/install if you are only spending 2 hours on it.

如果您只花 2 小时,那么 1/2 小时的下载/安装是不值得的。

Both have hotkeys/menu items to "Fix imports", with this you should never have to worry about imports again.

两者都有“修复导入”的热键/菜单项,这样您就不必再担心导入了。

回答by krakatoa

The standard Java classloader is a stickler for directory structure. Each entry in the classpath is a directory or jar file (or zip file, really), which it then searches for the given class file. For example, if your classpath is ".;my.jar", it will search for com.example.Foo in the following locations:

标准 Java 类加载器是目录结构的坚持者。类路径中的每个条目都是一个目录或 jar 文件(或 zip 文件,实际上),然后它会搜索给定的类文件。例如,如果您的类路径是“.;my.jar”,它将在以下位置搜索 com.example.Foo:

./com/example/
my.jar:/com/example/

That is, it will look in the subdirectory that has the 'modified name' of the package, where '.' is replaced with the file separator.

也就是说,它将在具有包的“修改名称”的子目录中查找,其中“。” 替换为文件分隔符。

Also, it is noteworthy that you cannot nest .jar files.

此外,值得注意的是,您不能嵌套 .jar 文件。

回答by Michael Myers

Okay, just to clarify things that have already been posted.

好的,只是为了澄清已经发布的内容。

You should have the directory com, containing the directory company, containing the directory example, containing the file MyClass.java.

你应该有目录com,包含目录company,包含目录example,包含文件MyClass.java

From the folder containing com, run:

从包含 的文件夹中com,运行:

$ javac com\company\example\MyClass.java

Then:

然后:

$ java com.company.example.MyClass
Hello from MyClass!

These must both be done from the root of the source tree. Otherwise, javacand javawon't be able to find any other packages (in fact, javawouldn't even be able to run MyClass).

这些都必须从源树的根开始。否则,javac而且java将无法找到任何其他包(事实上,java甚至不能够运行MyClass)。

A short example

一个简短的例子

I created the folders "testpackage" and "testpackage2". Inside testpackage, I created TestPackageClass.java containing the following code:

我创建了文件夹“testpackage”和“testpackage2”。在 testpackage 中,我创建了包含以下代码的 TestPackageClass.java:

package testpackage;

import testpackage2.MyClass;

public class TestPackageClass {
    public static void main(String[] args) {
        System.out.println("Hello from testpackage.TestPackageClass!");
        System.out.println("Now accessing " + MyClass.NAME);
    }
}

Inside testpackage2, I created MyClass.java containing the following code:

在 testpackage2 中,我创建了包含以下代码的 MyClass.java:

package testpackage2;
public class MyClass {
    public static String NAME = "testpackage2.MyClass";
}

From the directory containing the two new folders, I ran:

从包含两个新文件夹的目录中,我运行:

C:\examples>javac testpackage\*.java

C:\examples>javac testpackage2\*.java

Then:

然后:

C:\examples>java testpackage.TestPackageClass
Hello from testpackage.TestPackageClass!
Now accessing testpackage2.MyClass

Does that make things any clearer?

这让事情变得更清楚了吗?

回答by cbinder

Just add classpath entry ( I mean your parent directory location) under System Variables and User Variables menu ... Follow : Right Click My Computer>Properties>Advanced>Environment Variables

只需在 System Variables and User Variables 菜单下添加类路径条目(我的意思是您的父目录位置)... 按照:右键单击我的电脑>属性>高级>环境变量