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

查看:166
本文介绍了如何运行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:\Users\Matt\workspace\HelloWorld2\bin
这是我走进命令提示符,并键入Java HelloWorld2:

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

C:\Users\Matt>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打印出来。我做错了什么?

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

推荐答案

您需要设置类路径以找到您编译的类:

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

java -cp C:\Users\Matt\workspace\HelloWorld2\bin HelloWorld2

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

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