使用 Notepad++ 编译 Java 代码
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4314027/
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
Using Notepad++ to compile Java code
提问by Joseph
I've been trying to set up Notepad++ as a little Java environment, mainly for learning Java as I was having some difficulty getting a simple program to work with NetBeans, unfortunately all the advice on setting up Notepad++ to call the Java code is not working.
我一直在尝试将 Notepad++ 设置为一个小的 Java 环境,主要是为了学习 Java,因为我在使用 NetBeans 的简单程序时遇到了一些困难,不幸的是,所有关于设置 Notepad++ 来调用 Java 代码的建议都不起作用.
I guess notepad++ has changed or the Java development Kit has been massively modified because all examples I have used result in errors, even though there is little room for error.
我猜 notepad++ 已经改变了或者 Java 开发工具包已经被大规模修改,因为我使用的所有示例都会导致错误,即使几乎没有错误空间。
to begin I found this site: http://blog.sanaulla.info/2008/07/25/using-notepad-to-compile-and-run-java-programs/
一开始我找到了这个网站:http: //blog.sanaulla.info/2008/07/25/using-notepad-to-compile-and-run-java-programs/
this is the code to run Javac to compile the code:
这是运行 Javac 来编译代码的代码:
javac “$(FILE_NAME)”
and
和
java “$(NAME_PART)”
to run the resulted byte code, however this has absolutely no success at all anymore. Java is properly setup and I can call the Java program to do its thing through CMD.
运行结果字节码,但是这完全没有成功。Java 设置正确,我可以通过 CMD 调用 Java 程序来完成它的工作。
Using a plugin called npp and called through F6 and run with this code (given in the comments) succeeds in compiling the Java program into the correct .class file, however the command failed in running the program
使用名为 npp 并通过 F6 调用并使用此代码(在注释中给出)运行的插件成功将 Java 程序编译为正确的 .class 文件,但是该命令无法运行该程序
cd “$(CURRENT_DIRECTORY)”
javac $(FILE_NAME)
java $(NAME_PART)
errors from the console in Notepad++ are:
Notepad++ 中控制台的错误是:
java.lang.NoClassDefFoundError: first
Caused by: java.lang.ClassNotFoundException: first
at java.net.URLClassLoader.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: first. Program will exit.
Exception in thread "main"
I figured setting up Notepad++ to compile and run the code would be easy and fun, but its seems all documentation on the internet is outdated as nothing works.
我认为设置 Notepad++ 来编译和运行代码会很容易也很有趣,但它似乎互联网上的所有文档都过时了,因为没有任何作用。
I would like a easy way to compile and run Java code from within Notepad++
我想要一种在 Notepad++ 中编译和运行 Java 代码的简单方法
I could just used CMD but i'd rather it be more integrated into notepad++
我可以只使用 CMD,但我宁愿它更多地集成到记事本++中
Thanks for any possible help. cheers :)
感谢您提供任何可能的帮助。欢呼:)
EDIT: I'm using the latest version of Java, notepad++ and have Windows 7
编辑:我正在使用最新版本的 Java、notepad++ 并拥有 Windows 7
EDIT 2: the code:
编辑2:代码:
//A Very Simple Example
class ExampleProgram {
public static void main(String[] args){
System.out.println("I'm a Simple Program");
}
}
采纳答案by Mark Mayo
The 'learning curve' associated with IDEs like Eclipse or Netbeans initially mostly involves what you already have above - knowledge of setting class paths, environment variables and so on. Instead of Notepad++ (which I love, but it's not 'made' for Java), I'd recommend Eclipse especially if you have a grunty PC (it's a bit memory hungry). Aside from getting the paths setup, after that you'll be ready to rock.
与 Eclipse 或 Netbeans 等 IDE 相关的“学习曲线”最初主要涉及您在上面已经掌握的知识 - 设置类路径、环境变量等的知识。而不是 Notepad++(我喜欢它,但它不是为 Java“制造”的),我推荐 Eclipse,尤其是如果你有一台破旧的 PC(它有点内存饥渴)。除了设置路径之外,之后您就可以开始摇滚了。
And Eclipse being actively and openly developed is one of the most documented IDEs out there. The tutorials are bound to work correctly for it :). But seriously, it's pretty good. And then when you want to expand to Android development in Java, or some other type of Java programming, you just load up the add-ins required, and you're away laughing. It also supports debugging, the likes of which Notepad++ certainly cannot compete.
Eclipse 正在积极和公开地开发,是目前记录最多的 IDE 之一。这些教程一定能正常工作:)。但说真的,这还不错。然后,当您想扩展到 Java 中的 Android 开发或其他类型的 Java 编程时,您只需加载所需的加载项,然后就笑不出来了。它还支持调试,Notepad++ 之类的肯定无法与之匹敌。
回答by kgiannakakis
Probably changing the last line to:
可能将最后一行更改为:
java -cp . $(NAME_PART)
will work for you. The problem is that you aren't setting up the classpath.
会为你工作。问题是您没有设置类路径。
Notepad++ will be fine for compiling a single file project. For anything more than this you will need an IDE or at least integrate with antinstead of java compiler.
Notepad++ 可以用于编译单个文件项目。除此之外,您将需要一个 IDE 或至少与ant而不是 java 编译器集成。
回答by dogbane
Set the classpath in the java command like this:
在 java 命令中设置类路径,如下所示:
java -classpath “$(CURRENT_DIRECTORY)” “$(NAME_PART)”
回答by sly7_7
Although I'm convinced that you have to work with an IDE (NetBeans, Eclipse, IntelliJ IDEA (which I use), I think it's always good to know and learn what is failing in your small example. With an IDE, the compile and runtime environment are configured, but as a developper, it's important to understand the basic concepts hidden. Anyway,
尽管我确信您必须使用 IDE(NetBeans、Eclipse、IntelliJ IDEA(我使用的),但我认为了解和了解您的小示例中的失败总是很好的。使用 IDE,编译和运行时环境已经配置好了,但是作为开发者,了解隐藏的基本概念很重要。无论如何,
From the link you've posted, here are the environnement variables you must define
从您发布的链接中,这里是您必须定义的环境变量
FULL_CURRENT_PATH: C:\Documents and Settings\Administrator\My Documents\JavaP\ExampleProgram.java
CURRENT_DIRECTORY: C:\Documents and Settings\Administrator\My Documents\JavaP\
FILE_NAME: ExampleProgram.java
NAME_PART: ExampleProgram
EXT_PART:java
Make sure that all is named according to these settings, ie:
确保所有都根据这些设置命名,即:
- your source file is under C:\Documents and Settings\Administrator\My Documents\JavaP\
- your source file is named ExampleProgram.java
回答by Boris Lopez
I agree with the accepted answer but I sometimes use Textpadto quickly write/compile/run small java programs. Textpad has this built-in (Tools/External Tools). If you don't see this options I think you have to go to Configure/Preferences/Tools and add them. Then you can just hit Ctrl-1 to compile and Ctrl-2 to run. This is useful for very small quick tests, no libraries or anything.
我同意接受的答案,但有时我会使用Textpad来快速编写/编译/运行小型 Java 程序。Textpad 有这个内置(工具/外部工具)。如果您没有看到此选项,我认为您必须转到配置/首选项/工具并添加它们。然后你可以直接按 Ctrl-1 编译,按 Ctrl-2 运行。这对于非常小的快速测试很有用,没有库或任何东西。
回答by oscarMg
You can use eclipse as suggested above, and just create a java project. After you create the project just drag and drop the java file you want to work with into the project and select the link file option. That way eclipse will create a copy of your file and link it to your file, meaning every change you make to one file will be copied to the other.
您可以按照上面的建议使用 eclipse,只需创建一个 java 项目。创建项目后,只需将要使用的 java 文件拖放到项目中,然后选择链接文件选项。这样 eclipse 将创建您的文件的副本并将其链接到您的文件,这意味着您对一个文件所做的每一次更改都将复制到另一个文件。
回答by Tanz87
I've recently run into this situation in Windows 7 64-bit. Notepad++ is a 32-bit program, so Windows has enabled "File System Redirection" on it and its plugins (including NppExec), as per http://msdn.microsoft.com/en-us/library/windows/desktop/aa384187(v=vs.85).aspx. I also had the latest 64-bit JDK 8.xx installed but an earlier 32-bit JRE 7.xx installed.
我最近在 Windows 7 64 位中遇到了这种情况。Notepad++ 是一个 32 位程序,因此 Windows 已在其及其插件(包括 NppExec)上启用“文件系统重定向”,根据http://msdn.microsoft.com/en-us/library/windows/desktop/aa384187 (v=vs.85).aspx。我还安装了最新的 64 位 JDK 8.xx,但安装了较早的 32 位 JRE 7.xx。
Now the JRE 7.xx installer had placed a copy (or hardlink, I haven't checked) of java.exe in its C:\Windows\system32 -- which is actually C:\Windows\SysWOW64\java.exe. This is not in the PATH of 64-bit applications like cmd.exe, but is in the (redirected) PATH of 32-bit applications.
现在,JRE 7.xx 安装程序已在其 C:\Windows\system32 中放置了 java.exe 的副本(或硬链接,我尚未检查)——实际上是 C:\Windows\SysWOW64\java.exe。这不在 64 位应用程序(如 cmd.exe)的 PATH 中,而是在 32 位应用程序的(重定向)PATH 中。
Then after I installed JDK 8.xx, the installer did not update my PATH so I added the JDK install location to the end of my PATH. From that point on the behavior I observed was:
然后在我安装 JDK 8.xx 之后,安装程序没有更新我的 PATH,所以我将 JDK 安装位置添加到我的 PATH 的末尾。从那时起,我观察到的行为是:
- From the Windows Command Prompt (cmd.exe, 64-bit) -- both javac.exe and java.exe were from the JDK 8.xx location (C:\Program Files\Java\jdk1.8.0_05\bin).
- From within Notepad++ (32-bit), the JDK version of javac.exe was getting invoked but the java.exe was actually being run from C:\Windows\SysWOW64\java.exe -- leading to this kind of loading problem.
- 从 Windows 命令提示符(cmd.exe,64 位)——javac.exe 和 java.exe 都来自 JDK 8.xx 位置(C:\Program Files\Java\jdk1.8.0_05\bin)。
- 在 Notepad++(32 位)中,javac.exe 的 JDK 版本被调用,但 java.exe 实际上是从 C:\Windows\SysWOW64\java.exe 运行的——导致这种加载问题。
The fix was to update or remove the 32-bit JRE.
修复是更新或删除 32 位 JRE。
回答by user147
You can try to add the system environment variable for the jdk bin path. when i ran java on notepad++ for first time, i also encountered similar issue.
可以尝试为jdk bin路径添加系统环境变量。当我第一次在 notepad++ 上运行 java 时,我也遇到了类似的问题。
回答by lgt945
My situation is similar to yours,
我的情况和你一样
I compiled and run the "hello world" application in cmd correctly, but in notepad++, I can only do the compile but can't run the class file。
我在cmd中正确编译并运行了“hello world”应用程序,但是在notepad++中,我只能编译而不能运行class文件。
The reason is that I installed jdk1.7, set the PATHto jdk1.7, and then I installed jdk1.6 too. So the jdk1.6 installer add java.exe to %systemroot%\System32 (SySWOW64 in x64 environment) but no javac.exe.
原因是我装了jdk1.7,把PATH设置为jdk1.7,然后我也装了jdk1.6。所以 jdk1.6 安装程序将 java.exe 添加到 %systemroot%\System32(x64 环境中的 SySWOW64)但没有 javac.exe。
While in cmd modem, system called javac.exe and java.exe in PATH, notepad++ called javac.exe in PATHbecause there are no javac in system32 and java.exe in system32. (You can run "javac -version" and "java -version" in notepad++ to find out)
而在cmd modem中,系统在PATH中调用javac.exe和java.exe ,notepad++在PATH中调用javac.exe,因为system32中没有javac,system32中没有java.exe。(你可以在记事本++中运行“javac -version”和“java -version”来查找)
So I deleted java.exe in system32and then npp compiled and run perfectly.
所以我删除了system32中的java.exe,然后npp编译运行完美。
Hope this can help anyone.
希望这可以帮助任何人。
回答by RooGi
you can use this little code in "run" module of notepad ++:
你可以在记事本++的“运行”模块中使用这个小代码:
cmd /k "cd /D "$(CURRENT_DIRECTORY)" & java "$(NAME_PART)""
note: this is the almost same code solution they used to run python in notepad++ as explained in this topic: How to Execute a Python File in Notepad ++?
注意:这是他们用来在记事本 ++ 中运行 python 的几乎相同的代码解决方案,如本主题中所述:如何在记事本 ++ 中执行 Python 文件?