Java compile and run using notepad++ and nppexec

23,216

Solution 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

Solution 2

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.

Solution 3

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)
Share:
23,216
SgtPooki
Author by

SgtPooki

#SOreadytohelp Programming/Scripting/Automation Hobbyist. Professional Software Engineer. Ex-PayPal expert. JavaScript, Machine Learning, Artificial Intelligence, Finance, Education, and Big Data lover.

Updated on November 22, 2020

Comments

  • SgtPooki
    SgtPooki over 3 years

    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...