Home > Articles > Using Notepad++ to Compile and Run Java Programs

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

The usage of Environment Variable:
$(ENVIRONMENT_VARIABLE)

Ex: Lets say we are editing “NotHelloWorld.java”

The 5 environment Variable that can be used are:
FULL_CURRENT_PATH: C:\Documents and Settings\Administrator\My Documents\JavaP\NotHelloWorld.java
CURRENT_DIRECTORY: C:\Documents and Settings\Administrator\My Documents\JavaP\
FILE_NAME: NotHelloWorld.java
NAME_PART:NotHelloWorld
EXT_PART:java

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)

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 Notepad++ environment.

Advertisements
  1. August 10, 2008 at 7:01 pm

    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.

    Keep the good work going.

    Regards,
    [b]PassingSCJP[/b]

    • Aravind
      October 22, 2010 at 1:53 pm

      @Passing SCJP: Blah.. Blah.. Blah..

      @Mohamed Sanaulla: Very useful Information… Thank you

  2. ashwin
    August 14, 2008 at 10:22 pm

    where do you get notepad ++??it is not there as default in windows xp.

  3. August 15, 2008 at 4:23 pm

    @Ashwin

    yeah it doesn;t come bundled with Windows. Google for “Notepad++”. The first link will take u to their site

  4. September 8, 2008 at 11:27 pm

    Some changes have to be made if in case the code involves packages and command line arguments.

  5. John
    September 24, 2008 at 10:35 pm

    i have problem with notepad++….
    it cannot compile my project, what should i do?
    which version of java is used for notepad++?
    i use JavaJDK 6u2, i install it at C:\Program files\Java

    help me plz

  6. September 27, 2008 at 7:47 am

    @John

    What is the error u r getting?
    When using Notepad++ the Output window which u get after compiling does not stay so one cannot come to know the compilation errors.

    Any Java version is supported. Notepad++ is just an alternative to the Command line compilation. If u r facing problems using Notepad++ u can use command line compilation instead.

  7. LightGeoDuck
    October 13, 2008 at 8:22 pm

    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.

  8. October 15, 2008 at 9:34 pm

    @LightGeoDuck
    Thanks for the Tidbit. I have explained the same in the post. You are absolutely right it makes the work lot easier.

  9. LightGeoDuck
    October 16, 2008 at 2:51 am

    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

    • Lisa
      July 31, 2009 at 2:12 am

      Took me a long time to find out that F6 runs NppExec, which was not installed by default for me.

  10. October 16, 2008 at 8:12 am

    LightGeoDuck,

    Initially thought it was the same thing. But after u’r recent comment i tried it out and found it to be lot different from what i have posted. Sorry for not interpreting u’r suggestion correctly.

    Thanks a lot for the suggestion. Will update my post to add u’r suggestion.

    Do suggest some areas where i can blog about.

  11. Kshitij
    October 22, 2008 at 12:25 am

    can i add a shortcut key to nPPexecute so tht
    everytime i press it…my file gets compiled
    automatically and i also get to c results of
    compilation in command prompt window

    • Rock Hyrax
      January 21, 2010 at 8:09 pm

      Under NppExec Advanced Options there’s a Menu items area. You can add the items there and then assign shortcut keys using the method described in the dialogue box.

  12. Muhammad Ali A
    November 3, 2008 at 8:19 am

    Hi
    I am using Notepad and would like to compile C, C++ program
    How may I do so ?
    What C compiler is preferred ?
    Thanks dude

  13. November 9, 2008 at 8:52 am

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

  14. December 11, 2008 at 7:41 pm

    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.

    you can download the plugin from the plugin page on the Notepad++ site using this link: http://notepad-plus.sourceforge.net/uk/download.php

    I had to use the beta version to allow it to work with notepad++ version 5.1.1 hopefully using the beta version will be stable 🙂

  15. January 31, 2009 at 2:06 am

    better option for me in one line using %JAVA_HOME%

    cmd /c “cd “$(CURRENT_DIRECTORY)” & %JAVA_HOME%\jdk1.6\bin\javac.exe $(FILE_NAME) ”

    Thanks mate for the nice post.

  16. CoolPolarBear
    February 5, 2009 at 10:51 am

    To Compile Java Code on the current document you have open while in Notepad++:

    javac “$(FULL_CURRENT_PATH)”

    To run the current java program you have open:

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

    Add some shortcut keys and it works like a charm.

  17. February 5, 2009 at 11:42 am

    CoolPolarBear,psidarad, Russell

    Thanks for the suggestions. Haven’t been finding time to write more. Meanwhile if u have any suggestions on how i could improve the blog. Suggestions for the articles- U are please welcome.

  18. FURQAN
    February 27, 2009 at 1:58 am

    IS THERE ANY WAY I CAN WRITE A BUILD SCRIPT FOR NOTEPAD++ TO COMPILE ALL JAVA CLASSES AT ONCE

  19. April 28, 2009 at 9:22 pm

    for me it works if i type

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

    with “$(CURRENT_DIRECTORY)” it assumes notepad++ directory

  20. Rodney Reid
    June 8, 2009 at 8:20 am

    Thank for the great post and resource

    – this is just what I was looking for (adapted use to perforce checkouts in notepad++)

  21. Daniel
    August 13, 2009 at 3:09 am

    Fantastic!!

  22. Hari
    August 22, 2009 at 2:49 pm

    This is really valuable information

  23. September 10, 2009 at 7:22 pm

    Hi! I was surfing and found your blog post… nice! I love your blog. 🙂 Cheers! Sandra. R.

  24. Michel
    September 23, 2009 at 2:23 am

    Thanks for the idea!

    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.

    1. Press F6 to open NPPExec plugin

    2. type:

    //save current file
    NPP_SAVE
    //switch to java classpath defined in windows system variable
    cd “$(SYS.CLASSPATH)”
    //compile current file (full_current_path) to java classpath (javac switch -d used)
    javac -d $(SYS.CLASSPATH) $(FULL_CURRENT_PATH)

    pressing enter after each command

    3. save as “Java – COMPILE” for future use
    4. press ok to use the script

    5. type:

    //save current file
    NPP_SAVE
    //switch to java classpath defined in windows system variable
    cd “$(SYS.CLASSPATH)”
    //compile current file (full_current_path) to java classpath (javac switch -d used)
    javac -d $(SYS.CLASSPATH) $(FULL_CURRENT_PATH)

    pressing enter after each command

    6. save as “Java – COMPILE & RUN” for future use
    7. press ok to use the script

    • October 2, 2009 at 5:48 pm

      Thanks Michel for the update. Will update u’r suggestion on the post.

  25. gunptk
    September 26, 2009 at 10:22 am

    thank you LightGeoDuck for your advice.
    it work.

  26. fkuo
    February 4, 2010 at 11:37 am

    Hi-
    When I type javac “$(FILE_NAME)” using F5 command, the javac.exe file is called but nothing happens. The command windows appears and then disappears quickly. What do you think is happening?

  27. Kyle
    March 15, 2010 at 9:46 pm

    I had to remove the quotes from $(CURRENT_DIRECTORY) to get the script to work in Notepad++ using Windows Vista.

  28. June 14, 2010 at 2:50 pm

    hi..
    nice info.. thanks

  29. BOMBER227
    July 7, 2010 at 9:43 am

    cmd /k cd “$(CURRENT_DIRECTORY)” && javac “$(FILE_NAME)”

    cmd /k cd “$(CURRENT_DIRECTORY)” && java “$(FILE_NAME)”

    use these. the pause the CMD window and will work witth any open document in notepad++

    • BOMBER227
      July 7, 2010 at 9:45 am

      ***SORRY made a mistake, fixed 🙂

      cmd /k cd “$(CURRENT_DIRECTORY)” && javac “$(FILE_NAME)”

      cmd /k cd “$(CURRENT_DIRECTORY)” && java “$(NAME_PART)”

      use these. the pause the CMD window and will work witth any open document in notepad++

      • Phi Long
        October 5, 2010 at 12:35 am

        thanks,it worked ! 😀

  30. Shalabh Vyas
    October 12, 2010 at 10:48 am

    Yes.It works using NppExecute. But how do you make the command prompt to stay or pause using the Run(F5) feature.

    • October 22, 2010 at 2:47 pm

      For this you can add a line in your java code which waits for input from the user. Then, until you press some key the command window will stay.

  31. Aravind
    October 22, 2010 at 1:58 pm

    Thank you

  32. Aravind
    October 23, 2010 at 12:25 pm
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: