无法在 IntelliJ IDEA 中编译和运行 Java 代码
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20241265/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Cant compile and run java code in IntelliJ IDEA
提问by Sergey Novikov
Using InteliJ IDEA version 12.1.6 I wanted to run the example from the Horstmann's 'Core Java' book:
使用 InteliJ IDEA 12.1.6 版,我想运行 Horstmann 的“Core Java”一书中的示例:
public class Welcome
{
public static void main(String[] args)
{
String[] greeting = new String[3];
greeting[0] = "Welcome to Core Java";
greeting[1] = "by Cay Horstmann";
greeting[2] = "and Gary Cornell";
for (String g : greeting)
System.out.println(g);
}
}
But I get the following error:
但我收到以下错误:
"C:\Program Files\Java\jdk1.7.0_45\bin\java" -Didea.launcher.port=7533 "-Didea.launcher.bin.path=C:\Program Files (x86)\IntelliJ IDEA\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program Files\Java\jdk1.7.0_45\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.7.0_45\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.7.0_45\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.7.0_45\jre\lib\jce.jar;C:\Program Files\Java\jdk1.7.0_45\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.7.0_45\jre\lib\jfxrt.jar;C:\Program Files\Java\jdk1.7.0_45\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.7.0_45\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.7.0_45\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.7.0_45\jre\lib\resources.jar;C:\Program Files\Java\jdk1.7.0_45\jre\lib\rt.jar;C:\Program Files\Java\jdk1.7.0_45\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.7.0_45\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.7.0_45\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.7.0_45\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.7.0_45\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.7.0_45\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.7.0_45\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.7.0_45\jre\lib\ext\zipfs.jar;C:\Program Files (x86)\IntelliJ IDEA\lib\idea_rt.jar" com.intellij.rt.execution.application.AppMain Welcome
Exception in thread "main" java.lang.ClassNotFoundException: Welcome
at java.net.URLClassLoader.run(URLClassLoader.java:366)
at java.net.URLClassLoader.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:190)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:113)
Process finished with exit code 1
At the same time when compiling and executing a file in the command line everything is working correctly. Excuse me for such a question, I just started learning Java. :)
同时在命令行中编译和执行文件时一切正常。请问这样的问题,我刚开始学习Java。:)
采纳答案by Seagull
Sergey, it's look like you are working wrong with Idea. There is a step-by-step tutorial at wiki, that describes some basic concepts, about roots, run configurations, sdk-s and dependencies. It will be good, if you look at it. (Exploring the project structureand Building(Running) the projectespecially)
谢尔盖,看来您在使用 Idea 时遇到了问题。wiki 上有一个分步教程,它描述了一些基本概念,包括根、运行配置、sdk-s 和依赖项。如果你看看它会很好。(特别是探索项目结构和构建(运行)项目)
IDE is a great helper to learn language, especially such intellegent one, as Idea, so, it will be a good start, to get familiar with it.
IDE是学习语言的好帮手,尤其是像Idea这样的智能语言,所以,熟悉它会是一个很好的开始。
Basically, for a start, all your code(not compiled classes) must be under the source roots, and runConfiguration must specify application entry point (class with static void main()
method).
基本上,首先,您的所有代码(未编译的类)都必须位于源根目录下,并且 runConfiguration 必须指定应用程序入口点(带有static void main()
方法的类)。
You shouldn't think about class files, jar archives or command line tools, IDEA can do all this for you.
您不应该考虑类文件、jar 档案或命令行工具,IDEA 可以为您完成所有这些。
Better concentrate on learning, as I think!
更好地专注于学习,正如我所想的!