本文介绍了如何通过javac/java编译/运行Java代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在下面有一个代码.我可以在NetBeans中正常编译和运行它.但是,使用javac/java时,我无法正常运行它.我想念什么?

I have a code below. I can compile and run it normally in NetBeans. But with javac/java, I cannot run it normally. What do I miss?

代码:

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/* the original code is from link: http://edu.qudong.com/safe/base/Javascript/shilidaima/20080514/12527.html */

package gendemo;

/**
 *
 * @author tomxue
 */

class Gen2 {

    private Object ob; //定义一个通用类型成员

    public Gen2(Object ob) {
        this.ob = ob;
    }

    public Object getOb() {
        return ob;
    }

    public void setOb(Object ob) {
        this.ob = ob;
    }

    public void showTyep() {
        System.out.println("T的实际类型是: " + ob.getClass().getName());
    }
}

public class GenDemo2 {

    public static void main(String[] args) {
        //定义类Gen2的一个Integer版本
        Gen2 intOb = new Gen2(new Integer(88));
        intOb.showTyep();
        int i = (Integer) intOb.getOb();
        System.out.println("value= " + i);

        System.out.println("----------------------------------");

        //定义类Gen2的一个String版本
        Gen2 strOb = new Gen2("Hello Gen!");
        strOb.showTyep();
        String s = (String) strOb.getOb();
        System.out.println("value= " + s);
    }
}

通过javac,编译后,我得到了以下结果.

By javac, after compiling, I got below result.

tomxue@ubuntu:~/test$ javac GenDemo2.java
tomxue@ubuntu:~/test$ ls
Gen2.class  GenDemo2.class  GenDemo2.java

然后,如果我这样运行它:

And then, if I run it like this:

tomxue@ubuntu:~/test$ java Gen2
Exception in thread "main" java.lang.NoClassDefFoundError: Gen2
Caused by: java.lang.ClassNotFoundException: Gen2
        at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:276)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
        at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319)
tomxue@ubuntu:~/test$ java GenDemo2
Exception in thread "main" java.lang.NoClassDefFoundError: GenDemo2
Caused by: java.lang.ClassNotFoundException: GenDemo2
        at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:276)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
        at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319)

这有什么问题?

推荐答案

问题是Java期望目录结构与程序包名称("gendemo")相匹配,因此它找不到您的类.将Java文件移动到名为gendemo的子目录中,然后使用javac gendemo/GenDemo2.java从顶层目录进行编译,然后使用java -cp . gendemo.GenDemo2运行它.

The problem is that java expects the directory structure to match the package name ("gendemo"), so it can't find your classes. move your java file into a sub-directory named gendemo, then compile it from the top directory using javac gendemo/GenDemo2.java and run it using java -cp . gendemo.GenDemo2.

这篇关于如何通过javac/java编译/运行Java代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-07 23:41