Site icon Experiences Unlimited

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

Exit mobile version