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 SettingsAdministratorMy DocumentsJavaPNotHelloWorld.java

CURRENT_DIRECTORY: C:Documents and SettingsAdministratorMy DocumentsJavaP

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

  1. save for future use

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

  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

84 thoughts on “Using Notepad++ to Compile and Run Java Programs”

  1. 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]

    Reply
  2. 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 filesJava

    help me plz

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

    Reply
  4. 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.

    Reply
    • i have a doubt here !!
      What if we are running a user defined package program, what commands we need to use for it?

      Reply
  5. 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

    Reply
  6. 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.

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

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

      Reply
  8. 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

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

    Reply
  10. 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 🙂

    Reply
  11. better option for me in one line using %JAVA_HOME%

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

    Thanks mate for the nice post.

    Reply
  12. 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.

    Reply
  13. 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.

    Reply
  14. for me it works if i type

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

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

    Reply
  15. Thank for the great post and resource

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

    Reply
  16. 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

    Reply
  17. 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?

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

    Reply
  19. 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++

    Reply
    • ***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++

      Reply
  20. 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

    Reply
  21. here is simple class

    public class Test{
    public static void main(String[] args){
    System.out.println(“Hessllo”);
    }
    }

    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 ================

    Any idea why id does this ???

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

      Reply
    • instead of -> cd “$(CURRENT_DIRECTORY)”

      type this

      cd $(CURRENT_DIRECTORY)

      I think upper stuff(with inverted commas) works for linux
      the below stuff works for windows xp
      It took me one hour just to figure that out.

      Reply
  22. this information is really very useful because i thought without Linux we can not run our program now it is possible i am very happy to visit this web site. Thank u.

    Reply
    • Never tried that and not sure if its supported. But you might as well develop a plugin which would do that 🙂

      Reply
  23. which is this notepad pls give me the link from which it is downloaded ??? i have windows 7 on its default notepad no run command is coming

    Reply
  24. Hi, I am a starter with these things but I found it very interesting..

    PLEASE help me to update my knowledge…

    Thanks.

    Reply
  25. hi

    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: “);

    // open up standard input
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

    String userName = null;

    // read the username from the command-line; need to use try/catch with the
    // readLine() method
    try {
    userName = br.readLine();
    } catch (IOException ioe) {
    System.out.println(“IO error trying to read your name!”);
    System.exit(1);
    }

    System.out.println(“Thanks for the name, ” + userName);

    }

    }

    Reply
    • 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)

      javac $(FILE_NAME)

      java $(NAME_PART)

      and there you have your program running as expected

      Reply
  26. I came up with this kind of setup if I need an JAR file within the compilation and running of Java:

    NPP_SAVE
    cd $(CURRENT_DIRECTORY)
    javac -classpath .;jr.jar $(FILE_NAME)
    java -classpath .;jr.jar $(NAME_PART)

    Somehow the -d option doesn’t work when there was multiple directories in it?

    Reply
  27. Hi, I have java prog and trying to run it in notepad++. Here is what I am executing line by line

    1.cd ($CURRUNT_DIRECTORY)
    2. When I ran javac $(FILE_NAME) or javac “$(FILE_NAME)”

    it ends up with following error

    javac FileProcessor.java
    CreateProcess() failed with error code 2: The system cannot find the file specified.

    Any comments will be appreciated.

    Thanks

    Sukhi

    Reply
  28. I use it that way:

    cd $(CURRENT_DIRECTORY)
    C:Program FilesJavajdk1.7.0_01binjavac $(FILE_NAME)
    C:Program FilesJavajdk1.7.0_01binjava $(NAME_PART)

    Reply
  29. using notepad in windows operation to compile executable command like for instance if u want the computer to speak just like this
    Dim message,sapi

    message=InputBox(“What do

    you want me to say?”,”Speak to

    Me”) Set sapi=CreateObject

    (“sapi.spvoice”) sapi.Speak

    Dim message
    how do u do more of this

    Reply
  30. I like to use the following method:

    Press F5 and type in the following all on one line:

    cmd /c cd $(CURRENT_DIRECTORY) && javac $(FILE_NAME) && java $(NAME_PART) & timeout -1>nul

    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.

    Reply
  31. NppExec didn’t work for me. For some reason java only runs when launched from cmd

    the only thing that worked for me was:
    cmd /k cd $(CURRENT_DIRECTORY) && java -cp . $(NAME_PART)

    in the Menu – Run

    Reply
  32. Hi! This is great! I have learnt about NppExec from this blog.
    I have been using this in Run:

    cmd /k “pushd “$(CURRENT_DIRECTORY)” && javac “$(FULL_CURRENT_PATH)” && java “$(NAME_PART)””

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

    =)

    -a Malaysian

    Reply
  33. 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.

    =)

    -a Malaysian

    Reply

Leave a Reply

Discover more from Experiences Unlimited

Subscribe now to keep reading and get access to the full archive.

Continue reading