java编译错误“无法找到或加载主类main.java" [英] java compiling error "could not find or load main class main.java"

查看:32
本文介绍了java编译错误“无法找到或加载主类main.java"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道有很多与此类似的线程,但我找不到解决我问题的线程.按照 this 指令,我能够在终端中获取 java 并能够编译.我能够javac main.java"没有错误,但是当我java main.java"时,它只是说它无法找到或加载主类main.java.我相信我的类路径是错误的,但我也不完全确定如何解决这个问题.这是我输入 echo $PATH

I understand there are a lot of threads similar to this one, but I couldn't find the one that solved my problem. Following this instruction I was able to get java in terminal and be able to compile. I am able to "javac main.java" with no errors, but when I "java main.java", it simply says it could not find or load main class main.java. I believe that my classpath is wrong but i'm not entirely sure how to fix this either. This is what comes out when I type in echo $PATH

/usr/lib/lightdm/lightdm:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/home/taka/.rvm/bin

当我输入 echo $CLASSPATH 时,它不显示任何内容.

and when I type echo $CLASSPATH it doesn't show anything.

我也尝试过 java -cp ./main.java ,因为当我编译并运行 HelloWorld.java 时,它似乎有效

I have also tried java -cp ./ main.java as that seemed to have worked when I compiled and ran HelloWorld.java

推荐答案

如果您的 javac 成功,则更新您的 classpath 环境变量并添加当前目录,即 .类路径,然后运行 ​​java 如下:

If your javac is successful then update your classpath environment variable and add current directory i.e. . in the classpath, then run the java as below:

 java main

请注意:没有 .java 扩展名,因为您需要运行 .class 文件(它是在 javac 之后生成的)没有提到扩展名.Java 使用生成的类文件执行而不是原始源文件.

Please note: There is no .java extension as you need to run .class file(which was generated after javac) that also without mentioning the extension. Java uses generated class files to execute not the original source files.

这篇关于java编译错误“无法找到或加载主类main.java"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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