Using Notepad++ to compile Java code

I’ve been trying to set up Notepad++ as a little Java environment, mainly for learning Java as I was having some difficulty getting a simple program to work with NetBeans, unfortunately all the advice on setting up Notepad++ to call the Java code is not working.

I guess notepad++ has changed or the Java development Kit has been massively modified because all examples I have used result in errors, even though there is little room for error.

to begin I found this site: http://blog.sanaulla.info/2008/07/25/using-notepad-to-compile-and-run-java-programs/

this is the code to run Javac to compile the code:

javac “$(FILE_NAME)”

and

java “$(NAME_PART)”

to run the resulted byte code, however this has absolutely no success at all anymore. Java is properly setup and I can call the Java program to do its thing through CMD.

Using a plugin called npp and called through F6 and run with this code (given in the comments) succeeds in compiling the Java program into the correct .class file, however the command failed in running the program

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

errors from the console in Notepad++ are:

java.lang.NoClassDefFoundError: first
Caused by: java.lang.ClassNotFoundException: first
  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: first.  Program will exit.
Exception in thread "main"

I figured setting up Notepad++ to compile and run the code would be easy and fun, but its seems all documentation on the internet is outdated as nothing works.

I would like a easy way to compile and run Java code from within Notepad++

I could just used CMD but i’d rather it be more integrated into notepad++

Thanks for any possible help. cheers 🙂

EDIT: I’m using the latest version of Java, notepad++ and have Windows 7

EDIT 2: the code:

 //A Very Simple Example
 class ExampleProgram {

   public static void main(String[] args){

        System.out.println("I'm a Simple Program");
   }
 }

how to compile java with external libraries in notepad++?

I’m using Notepad++ to learn Java. I have it set it up to compile and run Java from the Run menu, only that I have been coding small exercises without external libraries, I know I have to use -classpa

Using Regex to Quickly Add Code to Java Methods in Notepad++

I am looking for the regex which will let me use search replace to add 2 lines of code to all java methods in file using Notepad++ Before: public void startProcessing() { ………….. ………..

Compile files using Notepad++ and Intel Fortran Compiler

I’m newbie, using Notepad++, but I’m programming with Fortran Intel Compiler. I would like to compile and run my Fortran scripts using, directly, Notepad++. Could anyone help me to say me, what I need

Compile java code in run time using C#

I know it is possible to compile c# code at runtime using C#, with CSharpCodeProvider and CodeDom. Is it possible to compile also Java? If it isn’t, is there any alternative? I want my application to

Java Code Compile Error

I am trying to compile a 4KB Java game called Left 4K Dead.. anyways, It will compile successfully with the javac G.java command, but when you go to run it using java G it spits back this error at m

Java: Generate custom Java code at compile time using annotations

How do I write a Java inner class with custom properties at compile time using annotations? For instance, I want this : @Generate class Person { String firstname, lastname; } to generate: class Pe

How can I excute Notepad by Java code

I want to excute Notepad program in MS Windows by Java code to open my text file. Pls help me to do that.

Using ant to compile java code

I’m a little new to using ant, and currently, the way I make ant scripts is by auto-generating them through eclipse in order to produce runnable jar’s. The problem with this is that it only reads the

Problems running java in notepad++

I’m trying to compile and run a java program in notepad++ and I get error code 2, the system cannot find the file specified. Here is my program code: public class test { public static void main(String

CMake to compile java code

Is it possible to use CMake to compile and run java code? From command line the commands that I write on terminal is: javac -classpath theClasspath mainClass.java java -classpath theClasspath mainClas

Answers

Probably changing the last line to:

java -cp . $(NAME_PART)

will work for you. The problem is that you aren’t setting up the classpath.

Notepad++ will be fine for compiling a single file project. For anything more than this you will need an IDE or at least integrate with ant instead of java compiler.

The ‘learning curve’ associated with IDEs like Eclipse or Netbeans initially mostly involves what you already have above – knowledge of setting class paths, environment variables and so on. Instead of Notepad++ (which I love, but it’s not ‘made’ for Java), I’d recommend Eclipse especially if you have a grunty PC (it’s a bit memory hungry). Aside from getting the paths setup, after that you’ll be ready to rock.

And Eclipse being actively and openly developed is one of the most documented IDEs out there. The tutorials are bound to work correctly for it :). But seriously, it’s pretty good. And then when you want to expand to Android development in Java, or some other type of Java programming, you just load up the add-ins required, and you’re away laughing. It also supports debugging, the likes of which Notepad++ certainly cannot compete.

Set the classpath in the java command like this:

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

Is your java class inside a file named ExampleProgram.java?

Although I’m convinced that you have to work with an IDE (NetBeans, Eclipse, IntelliJ IDEA (which I use), I think it’s always good to know and learn what is failing in your small example. With an IDE, the compile and runtime environment are configured, but as a developper, it’s important to understand the basic concepts hidden. Anyway,

From the link you’ve posted, here are the environnement variables you must define

FULL_CURRENT_PATH: C:/Documents and Settings/Administrator/My Documents/JavaP/ExampleProgram.java
CURRENT_DIRECTORY: C:/Documents and Settings/Administrator/My Documents/JavaP/
FILE_NAME: ExampleProgram.java
NAME_PART: ExampleProgram
EXT_PART:java

Make sure that all is named according to these settings, ie:

- your source file is under C:/Documents and Settings/Administrator/My Documents/JavaP/
- your source file is named ExampleProgram.java

I wrote a simple plugin which allows you to compile and execute Java programs from Notepad++ itself. Link for the plugin.

I agree with the accepted answer but I sometimes use Textpad to quickly write/compile/run small java programs. Textpad has this built-in (Tools/External Tools). If you don’t see this options I think you have to go to Configure/Preferences/Tools and add them. Then you can just hit Ctrl-1 to compile and Ctrl-2 to run. This is useful for very small quick tests, no libraries or anything.

You can use eclipse as suggested above, and just create a java project. After you create the project just drag and drop the java file you want to work with into the project and select the link file option. That way eclipse will create a copy of your file and link it to your file, meaning every change you make to one file will be copied to the other.

I’ve recently run into this situation in Windows 7 64-bit. Notepad++ is a 32-bit program, so Windows has enabled “File System Redirection” on it and its plugins (including NppExec), as per http://msdn.microsoft.com/en-us/library/windows/desktop/aa384187(v=vs.85).aspx. I also had the latest 64-bit JDK 8.xx installed but an earlier 32-bit JRE 7.xx installed.

Now the JRE 7.xx installer had placed a copy (or hardlink, I haven’t checked) of java.exe in its C:/Windows/system32 — which is actually C:/Windows/SysWOW64/java.exe. This is not in the PATH of 64-bit applications like cmd.exe, but is in the (redirected) PATH of 32-bit applications.

Then after I installed JDK 8.xx, the installer did not update my PATH so I added the JDK install location to the end of my PATH. From that point on the behavior I observed was:

  • From the Windows Command Prompt (cmd.exe, 64-bit) — both javac.exe and java.exe were from the JDK 8.xx location (C:/Program Files/Java/jdk1.8.0_05/bin).
  • From within Notepad++ (32-bit), the JDK version of javac.exe was getting invoked but the java.exe was actually being run from C:/Windows/SysWOW64/java.exe — leading to this kind of loading problem.

The fix was to update or remove the 32-bit JRE.

You can try to add the system environment variable for the jdk bin path. when i ran java on notepad++ for first time, i also encountered similar issue.

My situation is similar to yours,

I compiled and run the “hello world” application in cmd correctly, but in notepad++, I can only do the compile but can’t run the class file。

The reason is that I installed jdk1.7, set the PATH to jdk1.7, and then I installed jdk1.6 too. So the jdk1.6 installer add java.exe to %systemroot%/System32 (SySWOW64 in x64 environment) but no javac.exe.

While in cmd modem, system called javac.exe and java.exe in PATH, notepad++ called javac.exe in PATH because there are no javac in system32 and java.exe in system32. (You can run “javac -version” and “java -version” in notepad++ to find out)

So I deleted java.exe in system32 and then npp compiled and run perfectly.

Hope this can help anyone.

you can use this little code in “run” module of notepad ++:

cmd /k "cd /D "$(CURRENT_DIRECTORY)" & java "$(NAME_PART)""

note: this is the almost same code solution they used to run python in notepad++ as explained in this topic: How to Execute a Python File in Notepad ++?