Using Notepad++ to compile Java code

眉间皱痕 提交于 2019-12-31 11:41:17

问题


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.

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.

to begin I found this site: 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 “$(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.

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

cd “$(CURRENT_DIRECTORY)”
javac $(FILE_NAME)
java $(NAME_PART)

errors from the console in Notepad++ are:

java.lang.NoClassDefFoundError: first
Caused by: java.lang.ClassNotFoundException: first
  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: 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.

I would like a easy way to compile and run Java code from within Notepad++

I could just used CMD but i'd rather it be more integrated into notepad++

Thanks for any possible help. cheers :)

EDIT: I'm using the latest version of Java, notepad++ and have Windows 7

EDIT 2: the code:

 //A Very Simple Example
 class ExampleProgram {

   public static void main(String[] args){

        System.out.println("I'm a Simple Program");
   }
 }

回答1:


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.

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.




回答2:


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 ant instead of java compiler.




回答3:


Set the classpath in the java command like this:

java -classpath “$(CURRENT_DIRECTORY)” “$(NAME_PART)”



回答4:


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,

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



回答5:


I agree with the accepted answer but I sometimes use Textpad to 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.




回答6:


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.




回答7:


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.

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.

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:

  • 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.

The fix was to update or remove the 32-bit JRE.




回答8:


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.




回答9:


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。

The reason is that I installed jdk1.7, set the PATH to 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.

While in cmd modem, system called javac.exe and java.exe in PATH, notepad++ called javac.exe in PATH because there are no javac in system32 and java.exe in system32. (You can run "javac -version" and "java -version" in notepad++ to find out)

So I deleted java.exe in system32 and then npp compiled and run perfectly.

Hope this can help anyone.




回答10:


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 ++?




回答11:


I found this link useful.

However, the NppExec seems comes with its own JRE, so I have changed the scripts to this (means you need to hard code java location):

cd $(CURRENT_DIRECTORY)
C:\App\Java\jdk1.8.0_112\bin\java -version
C:\App\Java\jdk1.8.0_112\bin\javac $(FILE_NAME)
C:\App\Java\jdk1.8.0_112\bin\java $(NAME_PART)


来源:https://stackoverflow.com/questions/4314027/using-notepad-to-compile-java-code

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!