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

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

问题描述

我是一名C ++开发人员 - 不是java开发人员,但必须让这段代码正常工作......



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

  package com.company.thing; 

class MyClass ...

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

  import com.company.thing。*; 

javac编译器失败,com.company不存在。 (即使我将它编译在与我刚刚制作包的类文件相同的目录中)



我确信我正在做一些愚蠢和愚蠢的事情。 / p>

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



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



我试过编译

  javac -classpath <亲本GT; client.java 

,结果是:

 包com.company不存在

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



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

  javac -cp< jarfile> * .java 

结果是:

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

编辑



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



编译没有设置类路径 - 只需

  javac * .java父目录中的

- 它工作正常。



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



编辑:



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



谢谢

解决方案

好的,只是为了澄清已有的东西已发布。



您应该拥有目录 com ,其中包含目录 company ,包含目录 example ,包含文件 MyClass.java



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

 
$ javac com \ company = \\ example \ MyClass.java

然后:

 
$ java com.company.example.MyClass
来自MyClass的Hello!

这些必须都是从源树的根目录完成的。否则, javac java 将无法找到任何其他包(事实上, java 甚至无法运行 MyClass )。



简短示例



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

  package testpackage; 

import testpackage2.MyClass;

公共类TestPackageClass {
public static void main(String [] args){
System.out.println(Hello from testpackage.TestPackageClass!);
System.out.println(正在访问+ MyClass.NAME);
}
}

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

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

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

 
C:\ example> javac testpackage \ * .java

C:\ example> javac testpackage2 \ * .java

然后:

 
C: \ example> java testpackage.TestPackageClass
来自testpackage.TestPackageClass的Hello!
现在访问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 \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.

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 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, 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天全站免登陆