简体   繁体   English

Java不会在终端java.lang.NoClassDefFoundError中运行程序

[英]Java won't run program in terminal java.lang.NoClassDefFoundError

I'm following the java tutorials on the Princeton website . 我正在跟踪Princeton网站上的Java教程。

I'm running debian sqeeze 64bit and I have installed Sun java version 6. 我正在运行debian sqeeze 64bit并且已经安装了Sun Java版本6。

I can compile and run the basic hello world program without any problem, using the terminal and the Eclipse IDE. 我可以使用终端和Eclipse IDE毫无问题地编译并运行基本的hello world程序。

The problem is: 问题是:

when I try to compile and run a program, which requires an argument input for example: 当我尝试编译并运行程序时,这需要输入参数,例如:

public class RandomSeq { 
    public static void main(String[] args) {

        // command-line argument
        int N = Integer.parseInt(args[0]);

        // generate and print N numbers between 0 and 1
        for (int i = 0; i < N; i++) {
            System.out.println(Math.random());
        }
    }
}

I can run this on Eclipse putting an integer argument, however it doesn't work on the terminal. 我可以在Eclipse上放置一个整数参数来运行它,但是它在终端上不起作用。

I get this error: 我收到此错误:

emes@debian:~/Documents/workspace/IOput/src/randomSeq$ java RandomSeq 21 Exception in thread "main" java.lang.NoClassDefFoundError: RandomSeq (wrong name: randomSeq/RandomSeq) at java.lang.ClassLoader.defineClass1(Native Method) at java.lang.ClassLoader.defineClassCond(ClassLoader.java:631) at java.lang.ClassLoader.defineClass(ClassLoader.java:615) at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:141) at java.net.URLClassLoader.defineClass(URLClassLoader.java:283) at java.net.URLClassLoader.access$000(URLClassLoader.java:58) at java.net.URLClassLoader$1.run(URLClassLoader.java:197) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(URLClassLoader.java:190) at java.lang.ClassLoader.loadClass(ClassLoader.java:306) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301) at java.lang.ClassLoader.loadClass(ClassLoader.java:247) Could not find the main class: RandomSeq. emes @ debian:〜/ Documents / workspace / IOput / src / randomSeq $ java RandomSeq 21线程“ main”中的异常java.lang.NoClassDefFoundError:java.lang.ClassLoader.defineClass1(Native)上的RandomSeq(错误名称:randomSeq / RandomSeq)方法)在java.lang.ClassLoader.defineClassCond(ClassLoader.java:631)在java.lang.ClassLoader.defineClass(ClassLoader.java:615)在java.security.SecureClassLoader.defineClass(SecureClassLoader.java:141)在java。 net.URLClassLoader.defineClass(URLClassLoader.java:283)在java.net.URLClassLoader.access $ 000(URLClassLoader.java:58)在java.net.URLClassLoader $ 1.run(URLClassLoader.java:197)在java.security.AccessController .doPrivileged(本机方法)(java.net.URLClassLoader.findClass(URLClassLoader.java:190),java.lang.ClassLoader.loadClass(ClassLoader.java:306),位于sun.misc.Launcher $ AppClassLoader.loadClass(Launcher.java) :301),位于java.lang.ClassLoader.loadClass(ClassLoader.java:247)找不到主类:RandomSeq。 Program will exit. 程序将会退出。

I've tried to update the /etc/profile to include the java-6-sun on the PATH variable. 我试图更新/etc/profile以将java-6-sun在PATH变量中。

I'm not sure, what to try from here. 我不确定如何从这里尝试。

Apparently, you're trying to run your program from the src folder of an Eclipse project. 显然,您正在尝试从Eclipse项目的src文件夹中运行程序。 src stands for “source”. src代表“源”。 The executable version of your program (the compiled classes) is not in src ; 程序的可执行版本(编译的类)不在src it's in bin , which stands for “binary”, ie machine code. 它在bin ,代表“二进制”,即机器代码。

When using the command line, you should first compile your program: 使用命令行时,应首先编译程序:

javac MyClass.java

and then run it: 然后运行它:

java MyClass

But please, do not do it inside an Eclipse project's directory structure, or you will create additional files (class files) not expected by Eclipse at this location. 但是请不要在Eclipse项目的目录结构中执行此操作,否则您将在此位置创建Eclipse不需要的其他文件(类文件)。


Additionally maybe you are inside a package. 另外,也许您在包装内。 You can't run a class if you're within its package folder. 如果您在类的包文件夹中,则无法运行该类。 You need to be at the top-level of the package hierarchy. 您需要处于程序包层次结构的顶层。

Example: suppose your class is inside a package named mypackage . 示例:假设您的类在名为mypackage的包内。 Then in someFolder/mypackage/MyClass.java you will have something like: 然后在someFolder/mypackage/MyClass.java您将看到类似以下内容的内容:

package mypackage;

class MyClass {
    ...
}

After compiling your code, you must be in somefolder and issue the shell command: 编译代码后,您必须在somefolder并发出shell命令:

java mypackage.MyClass

It looks as if your class has a package 看来您的班级有一个礼包

package randomSeq;

public class RandomSeq {

If so, then when starting it it should be located in the folder randomSeq and the root of that folder should be in your class path and the package must be specified when invoking. 如果是这样,则在启动它时,它应该位于randomSeq文件夹中,并且该文件夹的根目录应该在您的类路径中,并且在调用时必须指定包。

So, if your .class file is in bin/randomSeq then you could run it with java -cp bin randomSeq.RandomSeq 21 因此,如果您的.class文件位于bin / randomSeq中,则可以使用java -cp bin randomSeq.RandomSeq 21运行它。

Don't bother about the argument as that would give a run-time null pointer exception. 不要理会参数,因为这会给运行时空指针带来异常。
The problem is your classpath. 问题是您的类路径。

Make a list (ls or dir) in the directory you run java RandomSeq from. 在运行Java RandomSeq的目录中列出一个列表(ls或dir)。 Do you have a .class file there. 你在那里有一个.class file If not run javac RandomSeq.java first to generate the class file 如果没有运行javac RandomSeq.java运行javac RandomSeq.java来生成类文件

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

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