Java compile and run using notepad++ and nppexec

霸气de小男生 提交于 2019-12-12 15:14:33

问题


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


回答1:


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.




回答2:


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)



回答3:


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




回答4:


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)"




回答5:


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)


来源:https://stackoverflow.com/questions/17204147/java-compile-and-run-using-notepad-and-nppexec

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!