4.7.2. Compiling Java

[This article uses some text originally posted by Sam (sammi101082) in Notepad++'es Help forum.]

[ Sam: ]

To compile a Java file, all you have to do is the following:

  1. Navigate to Plugins -> NppExec -> Execute. (Default Shortcut key is F6)
  2. In the new popup window, type this
    NPE_CONSOLE local -- x+ // enable the built-in error highlight filter locally
    cmd /c cd "$(CURRENT_DIRECTORY)" && "%JAVA_HOME%\bin\javac" "$(FULL_CURRENT_PATH)"  
  3. Click on "Save" and give it a name say "Java Compile".
  4. Bingo. You are there. Now any Java file you are editing in Notepad++ can be compiled by pressing F6 and choosing this script "Java Compile". It will compile the Java class for you. This is irrespective of which directory you are in.

The NppExec plugin can be used to execute any executable as you would from a command prompt. For running the compiled Java class, the script is:

NPE_CONSOLE local -- x+ // enable the built-in error highlight filter locally
cmd /k cd "$(CURRENT_DIRECTORY)" && "%JAVA_HOME%\bin\java" "$(NAME_PART)" && exit 

P.S. Remember to set the JAVA_HOME environment variable. To check if it is set, open a command prompt and type java.

[ DV: ]

Actually, the same thing can be done without explicit usage of 'cmd.exe'. You can use several commands (one command per line) in NppExec's script, and these commands will be executed one by one. For example:

NPE_CONSOLE local -- x+ // enable the built-in error highlight filter locally
cd "$(CURRENT_DIRECTORY)" 
"$(SYS.JAVA_HOME)\bin\javac" "$(FULL_CURRENT_PATH)" 

Also you can use ENV_SET to set environment variables such as PATH or JAVA_HOME for any process (program) you start from Notepad++ [4.2].