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.
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]
@Passing SCJP: Blah.. Blah.. Blah..
@Mohamed Sanaulla: Very useful Information… Thank you
Thanks Aravind!
where do you get notepad ++??it is not there as default in windows xp.
@Ashwin
yeah it doesn;t come bundled with Windows. Google for “Notepad++”. The first link will take u to their site
Some changes have to be made if in case the code involves packages and command line arguments.
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
@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.
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.
@LightGeoDuck
Thanks for the Tidbit. I have explained the same in the post. You are absolutely right it makes the work lot easier.
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
Took me a long time to find out that F6 runs NppExec, which was not installed by default for me.
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.
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
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.
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
@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.
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 🙂
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.
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.
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.
IS THERE ANY WAY I CAN WRITE A BUILD SCRIPT FOR NOTEPAD++ TO COMPILE ALL JAVA CLASSES AT ONCE
for me it works if i type
cd “$(LEFT_VIEW_FILE)”
javac $(FILE_NAME)
java $(NAME_PART)
with “$(CURRENT_DIRECTORY)” it assumes notepad++ directory
Thank for the great post and resource
– this is just what I was looking for (adapted use to perforce checkouts in notepad++)
Fantastic!!
This is really valuable information
Hi! I was surfing and found your blog post… nice! I love your blog. 🙂 Cheers! Sandra. R.
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
Thanks Michel for the update. Will update u’r suggestion on the post.
thank you LightGeoDuck for your advice.
it work.
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?
I had to remove the quotes from $(CURRENT_DIRECTORY) to get the script to work in Notepad++ using Windows Vista.
hi..
nice info.. thanks
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++
***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++
thanks,it worked ! 😀
Yes.It works using NppExecute. But how do you make the command prompt to stay or pause using the Run(F5) feature.
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.
Thank you
http://startclass0830.blogspot.com/2009/04/java-compile-and-run-in-notepad.html
Here is a better solution I guess