无法运行简单编译的java程序? [英] Cannot run simple compiled java program?

查看:32
本文介绍了无法运行简单编译的java程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Arch Linux 上,我刚刚安装了 JRE 和 JDK,所有正确的 bin 文件(javac 和 java)都在/opt/java/bin/中

I am on Arch Linux, I just installed JRE and JDK and all the proper bin files (javac and java) are in /opt/java/bin/

我只是编译了一个标准的 hello world,然后使用运行 javac ./hello.java 的 javac 编译它,然后生成了一个类.

I simply compiled a standard hello world, and compiled it with javac running javac ./hello.java and that made a class.

现在我的问题是运行它.我运行 java ./helloworld.class 并且它给了我一个错误,即使我指向 java 的文件不存在:

Now my problem is running it. I run java ./helloworld.class and it gives me an error, even if the file I point java to is non-existant:

Exception in thread "main" java.lang.NoClassDefFoundError: //helloworld/class
Caused by: java.lang.ClassNotFoundException: ..helloworld.class
(..omitted for clarity..)
Could not find the main class: ./helloworld.class.  Program will exit.

你会注意到错误的第一行,它修改了路径//helloworld/class

You will notice the first line of the error, it munges the path //helloworld/class

当我向 java 提供绝对路径时,即 java/home/foo/helloworld.class 它给出了相同的错误,但将路径的 / 替换为 . 在第一行,再次修改.

When I feed java an absolute path, i.e java /home/foo/helloworld.class it gives the same error, but replaces the path's / with . in the first line, again munged.

你觉得哪里不对?我真的不知道它为什么要这样做..

What do you think is wrong? I really don't know why it is doing this..

推荐答案

当你运行 java 时,你只是传递给它完全限定的类名(包括包),而不是文件名.

When you run java, you just pass it the fully qualified class name (including package), not the file name.

java helloworld 将查找 helloworld.class.

java helloworld will look for helloworld.class.

java helloworld.class 会寻找 helloworld/class.class

java helloworld.class will look for helloworld/class.class

这篇关于无法运行简单编译的java程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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