在Java中从控制台打开包 [英] Opening class in package from console in Java

查看:140
本文介绍了在Java中从控制台打开包的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,当我尝试从命令提示符打开一个不在包中的java类时,一切正常,但是当我尝试打开包中的类时,它会给我NoClassDefFoundError。
当我尝试打开类(java somepackage / someclass)时列出包时,它表示它无法加载或找到主类。

So when i try to open a java class that's not in a package from the command prompt it all works fine, but when I try to open a class that's in a package it gives me NoClassDefFoundError. And when I list the package when I try to open the class (java somepackage/someclass) it says that it can't load or find the main class.

任何帮助?

推荐答案

我可以推断,你有两个班级:

What I can infer is, you have two classes:

Test.java:

Test.java:

// no package defined here

class Test{
    public static void main(String[] args){
        System.out.println("Test");
    }
}

所以你可以使用以下代码编译和运行它: p>

so you can compile and run it using:

javac Test.java
java Test

另一个类:

Test.java:

Test.java:

package test; // package defined here

class Test{
    public static void main(String[] args){
        System.out.println("Test");
    }
}

因此做同样的事情会给你带来错误。
为此,您需要位于终端或cmd中'test'文件夹的父目录中并使用:

And thus doing same thing gives you error. For this you need to be in the parent directory of 'test' folder in your terminal or cmd and use:

java test.Test

编译器没问题。您可以使用javac Test.java从'test'文件夹中照常编译。

No problem with compiler. You can compile as usual using javac Test.java from inside 'test' folder.

这篇关于在Java中从控制台打开包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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