java中的包帮助 - 导入不起作用 [英] Help with packages in java - import does not work

查看:18
本文介绍了java中的包帮助 - 导入不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

package com.company.thing;类 MyClass ...

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

import com.company.thing.*;

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

我确定我在做一些愚蠢而愚蠢的事情.

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

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

我尝试编译

javac -classpath 客户端.java

结果是:

包 com.company 不存在

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

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

javac -cp *.java

结果是:

无法访问 MyClass错误的类文件:MyClass.class(:MyClass.class)类文件包含错误的类:com.company.product.MyClass请删除或确保它出现在类路径的正确子目录中.

编辑

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

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

javac *.java

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

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

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

谢谢

解决方案

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

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

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

<前>$ javac comcompanyexampleMyClass.java

那么:

<前>$ java com.company.example.MyClass你好,来自 MyClass!

这些都必须从源代码树的根开始.否则,javacjava 将无法找到任何其他包(实际上,java 甚至无法运行 MyClass).

一个简短的例子

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

package testpackage;导入 testpackage2.MyClass;公共类 TestPackageClass {公共静态无效主(字符串 [] args){System.out.println("Hello from testpackage.TestPackageClass!");System.out.println("现在访问" + MyClass.NAME);}}

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

package testpackage2;公共类 MyClass {public static String NAME =testpackage2.MyClass";}

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

<前>C:examples>javac testpackage*.javaC:examples>javac testpackage2*.java

那么:

<前>C:examples>java testpackage.TestPackageClass你好,来自 testpackage.TestPackageClass!现在访问 testpackage2.MyClass

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

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

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

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)

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.html pages 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.

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 comcompanyproduct. 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.

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

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.

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

解决方案

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.

From the folder containing com, run:

$ javac comcompanyexampleMyClass.java

Then:

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

These must both be done from the root of the source tree. Otherwise, javac and java won't be able to find any other packages (in fact, java wouldn't even be able to run MyClass).

A short example

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

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:

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?

这篇关于java中的包帮助 - 导入不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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