25

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");
   }
 }
Joseph
  • 3,899
  • 10
  • 33
  • 52
  • 1
    The obvious question is: Why use Notepad++ when you have NetBeans installed? – spender Nov 30 '10 at 13:03
  • 2
    Try using an IDE, i.e. Eclipse, which is built for writing code. Will teach you a lot more then notepad will. – Sean Nov 30 '10 at 13:04
  • 1
    Netbeans will make your life much easier. If you are having problems with it, just ask here – npinti Nov 30 '10 at 13:06
  • 2
    I posted a earlier question about how to get netbeans to work with simple single java files and I was advised to use notepad or notepad++ as there is a learning curve to using Netbeans. – Joseph Nov 30 '10 at 13:06
  • http://stackoverflow.com/questions/4309402/trying-to-follow-along-with-simple-java-tutorial-netbeans-not-compiling-code – Joseph Nov 30 '10 at 13:07
  • post the java code you are trying to compile; debugger is trying to say that it can't find your main file. Make sure that classpath is set ofc – bbaja42 Nov 30 '10 at 13:09
  • Netbeans and Eclipse might have a bigger learning curve, but it saves you a lot of frustration. – Mark Baijens Nov 30 '10 at 13:11
  • In your example, make sure that your file is named ExampleProgram.java – sly7_7 Nov 30 '10 at 13:13
  • I changed the name to the correct one but no difference, i think i'll stick with Netbeans, thanks for the effort though, cheers – Joseph Nov 30 '10 at 13:16
  • http://www.cs.auckland.ac.nz/courses/compsci101s1c/resources/Notepad/Notepad++.pdf This is it! –  Dec 29 '11 at 18:18
  • [This](http://blog.prateeksingla.com/2013/06/compile-and-run-java-programmes-in.html) might be helpful. – Prateek Singla Jun 07 '13 at 12:37

13 Answers13

16

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.

Mark Mayo
  • 12,230
  • 12
  • 54
  • 85
  • 3
    while i agree w/ Mark about the overall utility of an IDE, but I surely feel that it's quite an overkill to launch a bulky IDE, create a project, to do something as simple as writing a single class to check something. – anirvan Nov 30 '10 at 13:24
  • 2
    I disagree. I do simple concept testing all the time in Eclipse. In fact, I have a project called Test, with a single class, called Test, with a single function, main(), to do exactly that. Easy as pie. – aksarben Dec 01 '10 at 16:39
  • 1
    However, in theory, Java should not "BE" Eclipse, just like Visual Studio should not "BE" VB or C#. Yet that does seem to happen. By contrast, I haven't yet heard which IDE is going to "BE" javascript. Is that simply a matter of compiled vs. interpreted? A modern nodejs and jquery-lib web app can have 100s of source code files, but the IDE is usually, still, a slim text editor. Why the diff? – KTys Oct 09 '13 at 16:41
  • 2
    I think is a good practice for beginners to learn how to compile programs at hand, without the help of an IDE. After you know it well you can save time and effort using the IDE. – Roberto Linares Jun 30 '14 at 14:36
  • Don't forget IntelliJ , its my favorite. I believe Eclipse is recommended by Oracle but there are a few IDE's that work well with Java. – SparkleGoat Dec 22 '16 at 20:26
2

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.

kgiannakakis
  • 103,016
  • 27
  • 158
  • 194
  • I suppose it would be best to go back to NetBeans, getting the feeling that notepad++ is quite limited for larger projects, hmmm – Joseph Nov 30 '10 at 13:12
  • the edit made no difference, exactly the same error, I think that the function in NPP, $(NAME_PART) is broken – Joseph Nov 30 '10 at 13:14
  • 3
    Joseph, my friendly advise as programmer to programmer, is never to think that something is broken in the program/api/language you are using. In 99.999% of situations the problem is in what you are doing with it. – kgiannakakis Nov 30 '10 at 14:17
1

Set the classpath in the java command like this:

java -classpath “$(CURRENT_DIRECTORY)” “$(NAME_PART)”
dogbane
  • 266,786
  • 75
  • 396
  • 414
1

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
sly7_7
  • 11,961
  • 3
  • 40
  • 54
  • His class name is `ExampleProgram` so the file name should be `ExampleProgram.java`, not `NotHelloWorld.java`. Did you just copy-paste file names from the link he provided? – Peter Knego Nov 30 '10 at 13:35
  • I copy-paste for the example. If it's better I make the adaptation. it's made :), thx – sly7_7 Nov 30 '10 at 13:38
0

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.

Boris Lopez
  • 516
  • 8
  • 14
0

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.

oscarMg
  • 19
  • 2
0

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.

Tanz87
  • 1,111
  • 11
  • 10
0

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.

rahul pasricha
  • 931
  • 1
  • 14
  • 35
0

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.

lgt945
  • 19
  • 3
0

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 ++?

Community
  • 1
  • 1
RooGi
  • 1
0

I found this link useful.

However, the NppExec seems comes with its own JRE, so I have changed the scripts to this (means you need to hard code java location):

cd $(CURRENT_DIRECTORY)
C:\App\Java\jdk1.8.0_112\bin\java -version
C:\App\Java\jdk1.8.0_112\bin\javac $(FILE_NAME)
C:\App\Java\jdk1.8.0_112\bin\java $(NAME_PART)
neolei
  • 1,798
  • 2
  • 18
  • 32
0
cmd /k "cd /D "$(CURRENT_DIRECTORY)" & javac "$(FILE_NAME)" & java "$(NAME_PART)""

Add above to Run commands It worked!

germanio
  • 861
  • 1
  • 17
  • 27
chandu
  • 1
0

Here are the steps that will help you compile and run Java program in Notepad++.

  1. Install Java Development Kit and Java Runtime Environment Requirements
  2. Install NPPExec Plugin in Notepad++
  3. Add a script for Compiling and Running Java
  4. Add Menu Item for NPPExec script (Optional, but nice to have).

NPPExec Script

Make sure that the Java JDK path is correct in the script below.

// save program
NPP_SAVE

// change directory
cd "$(CURRENT_DIRECTORY)"

// compile
"C:\Program Files\Java\jdk1.8.0_301\bin\javac.exe" $(FILE_NAME)

// execute
"C:\Program Files\Java\jdk1.8.0_301\bin\java.exe" -cp  "$(CURRENT_DIRECTORY)"  "$(NAME_PART)"

Here is the step-by-step video demonstrating all of the above steps. (disclaimer: I am the owner of this channel)

NotepadPlusPlus PRO
  • 987
  • 1
  • 8
  • 21