如何运行 Java .class 文件? [英] How do I run Java .class files?

查看:20
本文介绍了如何运行 Java .class 文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经编译了一个 HelloWorld 程序,我正在使用命令提示符来运行它..class 文件名为 HelloWorld2.class

I've compiled a HelloWorld program, and I'm using the command prompt to run it. The .class file is named HelloWorld2.class

文件位于 C:UsersMattworkspaceHelloWorld2in这是我进入命令提示符并输入Java HelloWorld2"时得到的:

The file is located in C:UsersMattworkspaceHelloWorld2in Here's what I'm getting when I go to command prompt, and type "Java HelloWorld2" :

C:UsersMatt>Java HelloWorld2
Exception in thread "main" java.lang.NoClassDefFoundError: HelloWorld2
Caused by: java.lang.ClassNotFoundException: HelloWorld2
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
Could not find the main class: HelloWorld2.  Program will exit.

我期待看到打印出的 HelloWorld.我究竟做错了什么?我已经安装了 JDK.

I was expecting to see a HelloWorld printed out. What am I doing wrong? I have the JDK installed.

推荐答案

你需要设置classpath才能找到你编译的类:

You need to set the classpath to find your compiled class:

java -cp C:UsersMattworkspaceHelloWorld2in HelloWorld2

这篇关于如何运行 Java .class 文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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