简体   繁体   English

将类添加到包中

[英]adding class into package

I am studying java packages.我正在学习java包。 I successfully created a package pack that contained single .class file Hello.class.我成功创建了一个包含单个 .class 文件 Hello.class 的包包。 Now I want to add another class file into the same package.现在我想将另一个类文件添加到同一个包中。 I named the new java class as Goodbye.java and compiled it into the same directory "pack" via我将新的 java 类命名为 Goodbye.java 并通过以下方式将其编译到同一目录“pack”中

javac -d ./bin Goodbye.java

command.命令。 bin directory contains pack directory. bin 目录包含 pack 目录。 The I compile test.java file containing main function via我编译包含 main 函数的 test.java 文件通过

javac -cp ./bin test.java

command.命令。 Compilation works fine.编译工作正常。 But when I enter但是当我进入

java test 

command.命令。 I get我得到

Hello, world
Exception in thread "main" java.lang.NoClassDefFoundError: pack/Goodbye
at test.main(test.java:9)
Caused by: java.lang.ClassNotFoundException: pack.Goodbye
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
... 1 more

Can anybody explain what I did wrong in this case?谁能解释一下我在这种情况下做错了什么? I am working on ubuntu 14.04 and doing everything in the terminal.我正在 ubuntu 14.04 上工作并在终端中完成所有工作。

Both Hello.java and Goodbye.java files are located in Hello.java 和 Goodbye.java 文件都位于

/home/username/apps/src 

directory.目录。 Corresponding class files are inside里面有对应的class文件

/home/username/apps/bin/pack

directory.目录。 pack directory was created by包目录是由

javac -d ./bin 

command.命令。 Contents of the Hello.java file Hello.java 文件的内容

package pack;
public class Hello
{
    public static void HelloMessage()
    {
        System.out.println("hello, world");
    }
}

It works ok.它工作正常。 For its corresponding class file gets loaded Contents of Goodbye.java file is为其对应的类文件被加载 Goodbye.java 文件的内容是

package pack;
public class Goodbye
{
    public static void message()
    {
        System.out.println("bye");
    }
}

test.java file which imports the package is located at /home/username/apps directory.导入包的 test.java 文件位于 /home/username/apps 目录。 It has the following lines of code它有以下几行代码

import pack.*;
public class test
{
    public static void main(String args[])
    {
        Hello.HelloMessage();
        Goodbye.message();
    }
}

Any help is highly appreciated.任何帮助都受到高度赞赏。

My understanding of your class files is like the following tree:我对您的类文件的理解类似于以下树:

/home/username/apps/bin
                     |
                     +-- pack
                     |     |
                     |     +--- Hello.class
                     |     |
                     |     +--- Goodbye.class
                     |
                     +-- test.class

Then go to /home/username/apps/bin and call然后去/home/username/apps/bin并调用

java -cp . test

With -cp you add the current directory to search for classes.使用-cp添加当前目录以搜索类。 This should always be the root of your packages.这应该始终是您的包的根目录。 Then refer to your main class.然后参考你的主类。

BTW: According to the Java naming conventions , class names should start with a capital letter, like Test .顺便说一句:根据Java 命名约定,类名应该以大写字母开头,例如Test

Update : Updated the class file location.更新:更新了类文件位置。

You need to set classpath of two locations because you have class files in two different locations:您需要设置两个位置的类路径,因为您在两个不同的位置有类文件:

  1. /home/username/apps/ (test.class available in this location) /home/username/apps/(test.class 在这个位置可用)
  2. /home/username/apps/src/bin (pack.Hello.class and pack.Goodbye.class available in this location) /home/username/apps/src/bin(pack.Hello.class 和 pack.Goodbye.class 在这个位置可用)

You can do it by using -cp option of javac command.您可以使用 javac 命令的 -cp 选项来完成。 You need to put semicolon between more than one classpaths like below:您需要在多个类路径之间放置分号,如下所示:

java -cp .;./bin test

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM