简体   繁体   English

如何通过javac / java编译/运行java代码?

[英]How to compile/run the java code via javac/java?

I have a code below. 我有一个代码如下。 I can compile and run it normally in NetBeans. 我可以在NetBeans中正常编译和运行它。 But with javac/java, I cannot run it normally. 但是使用javac / java,我无法正常运行它。 What do I miss? 我错过了什么?

The code: 编码:

/*
 * 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);
    }
}

By javac, after compiling, I got below result. 通过javac,在编译之后,我得到了以下结果。

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)

What is wrong with it? 这有什么问题?

The problem is that java expects the directory structure to match the package name ("gendemo"), so it can't find your classes. 问题是java期望目录结构与包名称(“gendemo”)匹配,因此无法找到您的类。 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 将您的java文件移动到名为gendemo的子目录中,然后使用javac gendemo/GenDemo2.java从顶层目录编译它,并使用java -cp . gendemo.GenDemo2运行它java -cp . gendemo.GenDemo2 java -cp . gendemo.GenDemo2 . java -cp . gendemo.GenDemo2

When you run java SomeClass JVM load SomeClass and trying to run SomeClass.main method. 当你运行java SomeClass JVM加载SomeClass并尝试运行SomeClass.main方法时。

So you should run: 所以你应该运行:

 java GenDemo2 

or add: 或添加:

public static void main(String[] args) to Gen2.java

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

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