3

Please keep in mind that compiling in the windows shell works, so there is little (if not zero) possibility of this issue being a PATH issue.

I have spent a lot of time research how to do this, and all the results I found online say that you can do:

NPP_SAVE
javac $(FILE_NAME)
java $(NAME_PART)

but that does not work for me. In the NPP_EXEC console, I can type java, and I get the normal results as I would from cmd, but any time I type javac, I get the dreaded error code 2 error:

================ READY ================
javac
javac
CreateProcess() failed with error code 2:
The system cannot find the file specified.

================ READY ================

Edit

I must clarify some confusion:

  1. This solution should run in a single script. The goal is to be able to change code, press a hotkey combination (think F5 in Visual Studio) and it builds/compiles and runs.
  2. The actually issue, iirc, was that notepad++ is not recognizing javac for some reason..

Sorry for the confusion...

SgtPooki
  • 11,012
  • 5
  • 37
  • 46

5 Answers5

4

I have set it very easily by using this Article or you can also see another blog post which is very easy and helpful.

Now come to the point that how we can set the N++ and NppExec so our program run with on a single hand by N++.

Save this script first with the name of Java Compile

NPP_SAVE

cd "$(CURRENT_DIRECTORY)" 
"C:\Program Files (x86)\Java\jdk1.7.0\bin\javac" $(FILE_NAME)

here the main thing is your path of the java compiler, as in my case it is in C directory and most probably in the same of yours but still difference between 32bit and 64Bit OS.

Now save this scrip with another name like Compile and Run

cd "$(CURRENT_DIRECTORY)" 
"C:\Program Files (x86)\Java\jdk1.7.0\bin\java" -classpath "$(CURRENT_DIRECTORY)" "$(NAME_PART)"

Now add the script to the Macro in N++ to work it from there,

go to Advance Options within NppExec plugin,

A: Check the box at the top that says “Place to the Macros Submenu”

B: select script from “Associated Script” combo box. It will automatically fill in the “Item Name”

C: Now click the “Add/Modify” button.

D: Click OK. This will exit the Advanced Options box and say that NotePad++ needs to be restarted (don’t restart it until other scripts have been added).
We have to click OK because it’s the easiest way of clearing the boxes to add the next script otherwise it’s likely to overwrite the existing menu option.

E: Repeat these steps to add the other scripts and then restart it.

Its done now.

avirk
  • 3,050
  • 7
  • 38
  • 57
  • this is not a one script solution... it requires you to run two separate macros. – SgtPooki Jan 31 '14 at 02:11
  • @SgtPooki Can you tell me where did you mentioned that you don't want to run the 2 separate macros to not leave the N++, I accept it need two macros to run but it also doesn't leaving the N++ to run your java program. – avirk Jan 31 '14 at 02:35
  • The three commands in the first block quote imply that I need to 1.Save, 2. compile, 3. run in one script. Sorry for not being more clear. – SgtPooki Jan 31 '14 at 02:41
  • _As a side note_: if you have **JAVA_HOME** _environment variable_ set to point to your JDK installation folder (e.g. JAVA_HOME=C:\Program Files\Java\jdk1.8.0_91) and the following [added to your **PATH**](https://java.com/en/download/help/path.xml) environment variable (I prefer to put it in the beginning easily find later and to be sure it has the highest priority): `%JAVA_HOME%\bin;` then you don't need to specify full path in the related NPPExec file (i.e. you don't need to change it when updating JDK, just update JAVA_HOME). – informatik01 Jul 08 '16 at 15:06
4

My solution is adapted from the npp_exec help files (Plugins>Npp_Exec>Help/Manuals>Section 4.7.2). This works no problems for me and assumes that your JDK bin path has been added to the Windows system (or user) environment variable "Path".

NPP_SAVE
cd $(CURRENT_DIRECTORY)
javac $(FILE_NAME)
java $(NAME_PART)
dan
  • 239
  • 1
  • 2
  • 9
  • I'm not sure if there was a bug in nppexec when I attempted this or what, but that solution did not work for me. As you can see in my answer, javac was in the "path" but not accessible from notepad/nppexec for some reason, so I had to send the commands directly to the windows terminal. – SgtPooki Feb 26 '14 at 14:33
  • I believe that your issue was caused by the current working directory actually being the npp install directory (or similar not sure exactly what it defaults to) and not the directory that contained your java source file. Initially I was having similar 'file not found' errors until it dawned on me that javac was looking in the wrong place! Anyway, adding CD $(CURRENT_DIRECTORY) fixed the issue for me. – dan Mar 20 '14 at 11:38
1

I finally, after 5+ hours of googling and trial and error, have a working NPP Exec script that will compile and run a java program without leaving notepad++.

NPP_SAVE
cmd /K (javac "$(FULL_CURRENT_PATH)" && exit) || exit
cmd /K (cd /D "$(CURRENT_DIRECTORY)" && java $(NAME_PART) && exit) || exit

The only thing left would be finding a way to do the above, without having to call and send parameters to cmd, all in notepad++ and nppexec.

As noted in the comment below, if you're using a package, you will need to edit the second line accordingly. If your package name is the same as your file name, the below should work:

cmd /K (cd /D "$(CURRENT_DIRECTORY)" && java -cp .. $(NAME_PART).$(NAME_PART) && exit) || exit

SgtPooki
  • 11,012
  • 5
  • 37
  • 46
  • 1
    Thanks for the nice head start. I tried this but it didn't work for me. After some googling, it turned out that it was because of the fact that I'm using a package. This means I had to edit the 2nd command to look like this: `cmd /K (cd /D "$(CURRENT_DIRECTORY)" && java -cp .. $(NAME_PART).$(NAME_PART) && exit) || exit` And that is working for me. The package name being identical to the folder the file is in. – KdgDev Feb 25 '14 at 09:37
  • Oh and another thing. I noticed that the command doesn't work if you change the case of the package name. So basically, case-sensitivity can be a problem. – KdgDev Feb 27 '14 at 10:08
0

your origin command should work if you set the PATH correctly, the only thing you need to do is select the NppExec-Follow $(CURRENT_DIRECTORY) option, so that the npp can recognize your .java file.

Or you can also change $(FILE_NAME) to $(FULL_CURRENT_PATH) and change java $(NAME_PART) to :

cd $(CURRENT_DIRECTORY)

java "$(NAME_PART)"

lgt945
  • 19
  • 3
0

This Script on NppExec has worked in my case. Make sure that your path matches the Java version you installed on your machine.

NPP_SAVE 
cd $(CURRENT_DIRECTORY) 
C:\ProgramFiles\Java\jdk1.8.0_66\bin\javac $(FILE_NAME) 
C:\ProgramFiles\Java\jdk1.8.0_66\bin\java $(NAME_PART)
cryptic_star
  • 1,863
  • 3
  • 26
  • 47
Man Tamang
  • 49
  • 9