如何在java中编译包? [英] How to compile packages in java?

查看:76
本文介绍了如何在java中编译包?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个简单的程序包:

I have written a simple package program:

  //A simple package

       package MyPack


       class Balance
       {
        String name;
        double bal;

        Balance(String n, double b)
        {
            name=n;
                    bal=b;
            }

        void show()
            {
            if(bal<0)
            System.out.println("-->");
            System.out.println(name+ ": $:" +bal);
        }
        }

        class AccountBalance
        {
            public static void main(String args[])
            {
                       Balance current[]=new Balance[3];
                    current[0]=new Balance("A.K.Juwatkar",123.123);
                    current[1]=new Balance("A.P.Dhoke",345.67);
                    current[2]=new Balance("Anil Sarang",100.98);

                    for(int i=0;i<3;i++)
                current[i].show();
               }
        }






我正在使用Ubuntu 10.04&
当我使用


I am using Ubuntu 10.04 & When i compile it using

java MyPack.AccountBalance

我收到以下消息:

Exception in thread "main" java.lang.NoClassDefFoundError: MyPack/AccountBalance
Caused by: java.lang.ClassNotFoundException: MyPack.AccountBalance
    at java.net.URLClassLoader$1.run(URLClassLoader.java:217)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:321)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:266)
Could not find the main class: MyPack.AccountBalance. Program will exit.

有什么问题?请帮帮我。
我已经安装了openjdk,我还需要安装其他东西吗?
我正在使用Ubuntu 10.04,请帮助我

What is wrong? Please help me out. I have installed openjdk, do i need to install anything else?? I am using Ubuntu 10.04, kindly help me out

推荐答案

最好从外部编译和运行类包:

Best is to compile and run the classes from outside the packages :

首先用javac编译:

First you compile with javac :

$javac MyPack/AccountBalance.java

这将在MyPack文件夹中创建一个名为AccountBalance.class的新文件

this will create a new file in the MyPack folder called AccountBalance.class

然后你可以运行它:

$java MyPack.AccountBalance

顺便说一下:不鼓励包裹名称以大写字母开头。

By the way: it is discouraged to have package names start with a capital.

这篇关于如何在java中编译包?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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