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)
- (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.
-
Press F6 to open NPPExec plugin
-
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
-
save as “Java – COMPILE” for future use
-
press ok to use the script
-
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
-
save as “Java – COMPILE & RUN” for future use
-
press ok to use the script
Categories: Java
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]
LikeLiked by 1 person
@Passing SCJP: Blah.. Blah.. Blah..
@Mohamed Sanaulla: Very useful Information… Thank you
LikeLike
Thanks Aravind!
LikeLike
thanks to giving information
LikeLike
where do you get notepad ++??it is not there as default in windows xp.
LikeLike
@Ashwin
yeah it doesn;t come bundled with Windows. Google for “Notepad++”. The first link will take u to their site
LikeLike
Some changes have to be made if in case the code involves packages and command line arguments.
LikeLike
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
LikeLike
@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.
LikeLike
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.
LikeLiked by 1 person
@LightGeoDuck
Thanks for the Tidbit. I have explained the same in the post. You are absolutely right it makes the work lot easier.
LikeLike
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
LikeLike
Took me a long time to find out that F6 runs NppExec, which was not installed by default for me.
LikeLike
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.
LikeLike
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
LikeLike
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.
LikeLike
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
LikeLike
@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.
LikeLike
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 🙂
LikeLike
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.
LikeLike
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.
LikeLike
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.
LikeLike
IS THERE ANY WAY I CAN WRITE A BUILD SCRIPT FOR NOTEPAD++ TO COMPILE ALL JAVA CLASSES AT ONCE
LikeLike
for me it works if i type
cd “$(LEFT_VIEW_FILE)”
javac $(FILE_NAME)
java $(NAME_PART)
with “$(CURRENT_DIRECTORY)” it assumes notepad++ directory
LikeLike
Thank for the great post and resource
– this is just what I was looking for (adapted use to perforce checkouts in notepad++)
LikeLike
Fantastic!!
LikeLike
This is really valuable information
LikeLike
Hi! I was surfing and found your blog post… nice! I love your blog. 🙂 Cheers! Sandra. R.
LikeLike
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
LikeLike
Thanks Michel for the update. Will update u’r suggestion on the post.
LikeLike
Just updated with your method as well.
LikeLike
thank you LightGeoDuck for your advice.
it work.
LikeLike
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?
LikeLiked by 1 person
I had to remove the quotes from $(CURRENT_DIRECTORY) to get the script to work in Notepad++ using Windows Vista.
LikeLike
hi..
nice info.. thanks
LikeLike
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++
LikeLike
***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++
LikeLike
thanks,it worked ! 😀
LikeLike
Yes.It works using NppExecute. But how do you make the command prompt to stay or pause using the Run(F5) feature.
LikeLike
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.
LikeLike
Thank you
LikeLike
Here is a better solution I guess
LikeLike
thanks…:) this saved my time my notepad++ was installed on D drive so I couldn’t pass environmental variables properly…:(
LikeLike
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
LikeLike
Can you please try the other method- Suggested by one of our readers- Its towards the end of the post.
LikeLike
Use these. the pause the CMD window and will work witth any open fgdg f document in notepad.
LikeLike
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 ???
LikeLike
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++.
LikeLike
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.
LikeLike
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.
LikeLike
Thanks a lot for the kind words. You can always install JDK for Windows, configure the PATH variable and then use any of the editors like notepad and use command line to compile and run the programs. This link wil help you to get started: http://www.coderanch.com/how-to/java/how-to-create-java-program
LikeLike
If there is a compilation error, is it possible to click on the error line and make the editor go to that line?
LikeLike
Never tried that and not sure if its supported. But you might as well develop a plugin which would do that 🙂
LikeLike
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
LikeLike
You can download from here: http://notepad-plus-plus.org/
LikeLike
Hi, I am a starter with these things but I found it very interesting..
PLEASE help me to update my knowledge…
Thanks.
LikeLike
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);
}
}
LikeLike
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
LikeLike
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?
LikeLike
I havent tried using it with a lot of options. If someone has used it, would be helpful to highlight it here so that I can update the post.
LikeLike
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
LikeLike
You should be trying:
cd $(CURRENT_DIRECTORY)
javac $(FILE_NAME)
LikeLike
I use it that way:
cd $(CURRENT_DIRECTORY)
C:Program FilesJavajdk1.7.0_01binjavac $(FILE_NAME)
C:Program FilesJavajdk1.7.0_01binjava $(NAME_PART)
LikeLike
Great It works
LikeLike
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
LikeLike
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.
LikeLike
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
LikeLike
This plugin http://www.techsach.com/2012/06/java-tools-plugin-for-notepad.html does this for you.
LikeLike
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
LikeLike
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
LikeLike
I get error message “The system cannot find the file specified.” Please help.
LikeLike
I got the same message when I first tried to compile a program. Turns out I forgot to set the PATH environment variable. Try this: http://docs.oracle.com/javase/tutorial/essential/environment/paths.html.
LikeLike
Thank you
LikeLike
There is a notepad plugin to compile and run java program
download it here
LikeLike
Thank You. Its is really worth reading this blog. This link will help beginners to get started: http://www.javatrainingchennai.in/
LikeLike
very nice in java program injoy this
LikeLike
thanks that was very helpful
LikeLiked by 1 person