Menu

Using Notepad++ to Compile and Run Java Programs

I had been using Notepad– The one that comes with Windows for a long time for writing Java programs and then running it through command line, though i had Notepad++ installed on my system. But then it just flashed to me that “Why can’t i try Notepad++ for compiling and interpreting the Java programs“. I searched in the FAQs under “Running External Tools” and found useful information which has helped me to compile and run the Java programs right from the “Notepad++” application itself. Here’s how it can be done:

Running External Tools in Notepad++:

External tools can be executed by passing the current edited document as argument. For doing this the “environment variable” is used

Note: The environment variables must be enclosed with in double quotes.

“$(ENVIRONMENT_VARIABLE)”

For Java Compiler its: javac “$(FILE_NAME)”

For Running the Program i.e interpreting the byte code its: java “$(NAME_PART)”

The screenshots can guide u much better:

Above: Program Snapshot

Above: Using the “Run” Menu

Above: Typing in the “javac” command for Compiling

Above: Typing in the “java” command for interpreting the Byte Code

Above: The Output Obtained

One can click on “Save” to Save the commands, and the next time the same can be accessed through the “Run” menu.

Suggested Tidbit on Using Notepad++ by LightGeoDuck (One of the readers)

(after code is saved) press f6

type:

cd $(CURRENT_DIRECTORY)

javac $(FILE_NAME)

java $(NAME_PART)

pressing enter after each command

save for future use

press ok

If you are able to compile thru the PC cmd prompt this will do the same but with in the Notepad++ environment.

Note:

Using F6 to run would require NppExec plugin to be installed. So if its not installed the user may wonder why nothing is happening on pressing F6. One can install the plugin from the Plugins list in Notepad++ or download from http://notepad-plus.sourceforge.net/uk/download.php

Update: Suggested by one of the readers:

(Note: I haven’t tried this method. Please let me know if this doesn’t work or you have any issues)

I enhanced the script a little bit. Perhaps someone else has also use for this. It stores the *.class file in a different path (specified through the system variable CLASSPATH). If you work with a class containing a ‘public static void main(String[] args)’ method you can also test the class.

Post navigation

Hey mohammed, nice post I must say. The purpose of notepad when developed was just to provide a facility to scribble down and save it… a text tool. And that is what is provides very efficiently even today. It consumes very low memory and uses least system resources. When you want to run java rpograms now thats not a responsibility of a text tool… so you can switch over to more powerful tool and thats where IDE come into picture..
Anyways, it was a great post..also EditPlus is very easy use..i normally use it..compilation and running are just one mouse button click away.

I am new to Java myself, but here is a hopefully helpful tidbit. Since I am learning Java I want to try to avoid using an IDE, I use Notepad++ but I came to realize that it is sometimes a pain using the cmd prompt all of the time so I figured out this tip using NOTEPad++ for java.
1. (after code is saved) press f6
2. type: cd “$(CURRENT_DIRECTORY)”
javac $(FILE_NAME)
java $(NAME_PART)
pressing enter after each command
3. save for future use
4. press ok
IF you are able to compile thru the PC cmd prompt this will do the same but with in the NPpp environment.

MS,
Thanks for posting my comment, but maybe I didn’t explain it correctly. My comment is different from your post I believe. Where you used the Run command F5 where it would open the computers command prompt. I was refering to F6 where it puts the output inside the Npp editor. I attempted the F5 method and it didn’t work.for me that is

@Muhammad Ali A
Try using Cygwin. U can know more details about Cygwin -http://www.cygwin.com/
Once u have configured the bin directory path i.e added the bin directory of Cygwin to the existing path variable u can use it for compiling your C/C++ programs using gccg++ compiler on windows.

Just to clear up any confusion, if any readers find that pressing F6 does nothing, then you may not have the nppExec plugin installed. You need to install this before you can use the suggestion posted in the article.

I enhanced the script a little bit. Perhaps someone else has also use for this. It stores the *.class file in a different path (specified through the system variable CLASSPATH). If you work with a class containing a ‘public static void main(String[] args)’ method you can also test the class.

this things making me mad, i installed it on my flash drive and im trying to compile a Java program. I got to the run box and it say the program to run and I typed in javac “$(helloworld.java)*
and when I try to press the run button on the bottom of the box it dosent do anything, its like clicking on an icon or link and not doing anything or getting anywhere, it dosent load at all, can anyone please help me

here is waht i tyr to run as explained above
cd “$(CURRENT_DIRECTORY)”
javac $(FILE_NAME)
java $(NAME_PART)

and here is what i get
CD: “C:UsersYankoDesktop”
Current directory: C:Program FilesNotepad++
javac Test.java
Process started >>>
javac: file not found: Test.java
Usage: javac
use -help for a list of possible options
<<>>
java.lang.NoClassDefFoundError: Test
Caused by: java.lang.ClassNotFoundException: Test
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: Test. Program will exit.
Exception in thread “main” <<< Process finished.
================ READY ================

The problem is that the directory where you have stored the .java file and the working directory- the directory which javac/java command searches for the file are different. You would have to store you java source files in C:Program FilesNotepad++ and try running the commands.

You can even have a look at other commands/environment variables supported by Notepad++.

please help, i have been trying to run a java program to gets input from the keyboard however when i run the program it opens the source code file instead of show the input line to enter..below is the code i’m using:

import java.io.*;

public class ReadString {

public static void main (String[] args) {

// prompt the user to enter their name
System.out.print(“Enter your name: “);

Firstly see that you fix the compile time errors. For the strings in println you need to be using “”.
Then save the file with the name same as that of the class having main method.
Then install the NppExec plugin.
Press F6.
Type the following script in the pop up-
cd $(CURRENT_DIRECTORY)

Then hit Save… and assign a name and a Keyboard Shortcut (I use SHIFT + F5).

Now whenever you have saved code in your Notepad++ window, you can simply press SHIFT + F5 to both compile and run your program. This will only run the program if it compiles without errors, and there is an automatic pause built into the Run shortcut so you don’t have to build a pause into your program. As soon as you are done reviewing the output of your program (or compiler errors), you press a key and the command window closes! I like this method because it doesn’t require any plugins.

-C:Program Files (x86)Notepad++ or Notepad++ installation directory is local to the command run by Run.
So, if no pushd “$(CURRENT_DIRECTORY)”, java “$(NAME_PART)” will mean java C:Program Files (x86)Notepad++”$(NAME_PART)”.

And if you import class from somewhere not from JDK itself, then it won’t be able to compile as Run will look for the class in C:Program Files (x86)Notepad++, since C:Program Files (x86)Notepad++ is local to the command.

if you copy the code above, change the quote ” to “. The former one is invalid in the command line, it won’t work. For unknown reason the quotes are automatically changed to a different quotes character that will render the command invalid.

See the difference clearly.
” ”

PS: if no difference here, and it won’t work in Run, just retype the quotes anyway.