Java compile and run using notepad++ and nppexec - java

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:
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.
The actually issue, iirc, was that notepad++ is not recognizing javac for some reason..
Sorry for the confusion...

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.

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)

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

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

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)

Related

Java directory pathway

I wanted to start learning Java but I am a bit confused as to how to set the path correctly. I want to make a directory in my home directory for all my java programs so I was thinking something like:
/home/user/Java_Projects
but I'm not sure how to make the symbolic link to it. Would it be something like this?:
ln -sf /usr/local/jdk-11/bin/java /usr/local/bin/java/Java_Projects
Also, in this case what happens with javac? Does it then become:
ln -sf /usr/local/jdk-11/bin/javac /usr/local/bin/javac/Java_Projects
A strange thing that I've noticed is that when I run the code in my home directory (/home/user) everything works fine but then when I try to run the code from my java directory by changing to /home/user/Java_Projects it won't work anymore and I have no idea why.
The only thing I can think of is some kind of permission error because this is what I get:
error: error while writing HelloWorld: /home/user/Java_Projects/HelloWorld.class
Thanks in advance!
You're confusing the JDK tools (java, javac, etc) with the coding side. You will need to add /usr/local/jdk-11/bin to your PATH environment variable. Since you're in a Unix based environment you'll want to add that in your .bashrc or the equivalent for your shell. Something like:
export PATH=/usr/local/jdk-11/bin:$PATH
You normally have to start a new shell (i.e. the command line window) or log out and back in for that to take effect. Then, in your Java_projects directory you can simply run:
javac HelloWorld.java
You should have write permission in the Java_projects already assuming that it's in your home directory. If not, run chmod 755 Java_projects from the /home/user directory. The user in this case should be your login name to the operating system.

Code runs but doesn't open when converted to .jar file? [duplicate]

I want to execute my program without using an IDE.
I've created a jar file and an exectuable jar file. When
I double click the exe jar file, nothing happens, and when I try to use the command in cmd it gives me this:
Error: Unable to access jarfile <path>
I use the command: java -jar Calculator.jar
How I created the jar:
Right click on project folder (Calculator)
Select
Click on Java Folder and select "Exectuable Jar File", then select next
Launch Configuration: Main - Calculator
Create Export Destination
Hit "Finish" and profit! Well, not really.
I had encountered this issue when I had run my Jar file as
java -jar TestJar
instead of
java -jar TestJar.jar
Missing the extension .jar also causes this issue.
Fixed
I just placed it in a different folder and it worked.
[Possibly Windows only]
Beware of spaces in the path, even when your jar is in the current working directory. For example, for me this was failing:
java -jar myjar.jar
I was able to fix this by givng the full, quoted path to the jar:
java -jar "%~dp0\myjar.jar"
Credit goes to this answer for setting me on the right path....
I had this issue under CygWin in Windows. I have read elsewhere that Java does not understand the CygWin paths (/cygdrive/c/some/dir instead of C:\some\dir) - so I used a relative path instead: ../../some/dir/sbt-launch.jar.
I had the same issue when trying to launch the jar file. The path contained a space, so I had to place quotes around. Instead of:
java -jar C:\Path to File\myJar.jar
i had to write
java -jar "C:\Path to File\myJar.jar"
Just came across the same problem trying to make a bad USB...
I tried to run this command in admin cmd
java -jar c:\fw\ducky\duckencode.jar -I c:\fw\ducky\HelloWorld.txt -o c:\fw\ducky\inject.bin
But got this error:
Error: unable to access jarfile c:\fw\ducky\duckencode.jar
Solution
1st step
Right click the jarfile in question. Click properties.
Click the unblock tab in bottom right corner.
The file was blocked, because it was downloaded and not created on my PC.
2nd step
In the cmd I changed the directory to where the jar file is located.
cd C:\fw\ducky\
Then I typed dir and saw the file was named duckencode.jar.jar
So in cmd I changed the original command to reference the file with .jar.jar
java -jar c:\fw\ducky\duckencode.jar.jar -I c:\fw\ducky\HelloWorld.txt -o c:\fw\ducky\inject.bin
That command executed without error messages and the inject.bin I was trying to create was now located in the directory.
Hope this helps.
None of the provided answers worked for me on macOS 11 Big Sur. The problem turned out to be that programs require special permission to access the Desktop, Documents, and Downloads folders, and Java breaks both the exception for directly opened files and the permission request popup.
Fixes:
Move the .jar into a folder that isn’t (and isn’t under) Documents, Desktop, or Downloads.
Manually grant the permission. Go to System Preferences → Security and Privacy → Privacy → Files and Folders → java, and check the appropriate folders.
I had a similar problem and I even tried running my CMD with administrator rights, but it did not solve the problem.
The basic thing is to make sure to change the Directory in cmd to the current directory where your jar file is.
Do the following steps:
Copy jar file to Desktop.
Run CMD
Type command cd desktop
Then type java -jar filename.jar
This should work.
Edit: From JDK-11 onwards ( JEP 330: Launch Single-File Source-Code Programs )
Since Java 11, java command line tool has been able to run a single-file source-code directly. e.g.
java filename.java
If you are using OSX, downloaded files are tagged with a security flag that prevents unsigned applications from running.
to check this you can view extended attributes on the file
$ ls -l#
-rw-r--r--# 1 dave staff 17663235 13 Oct 11:08 server-0.28.2-java8.jar
com.apple.metadata:kMDItemWhereFroms 619
com.apple.quarantine 68
You can then clear the attributes with
xattr -c file.jar
It can also happen if you don't properly supply your list of parameters. Here's what I was doing:
java -jar test#gmail.com testing_subject file.txt test_send_emails.jar
Instead of the correct version:
java -jar test_send_emails.jar test#gmail.com testing_subject file.txt
This worked for me.
cd /path/to/the/jar/
java -jar ./Calculator.jar
For me it happens if you use native Polish chars in foldername that is in the PATH.
So maybe using untypical chars was the reason of the problem.
sometime it happens when you try to (run or create) a .jar file under /libs folder by right click it in android studio. you can select the dropdown in top of android stuio and change it to app. This will work
My particular issue was caused because I was working with directories that involved symbolic links (shortcuts). Consequently, trying java -jar ../../myJar.jar didn't work because I wasn't where I thought I was.
Disregarding relative file paths fixed it right up.
In my case the suggested file name to be used was jarFile*.jar in the command line. The file in the folder was jarFile-1.2.3.jar . So I renamed the file to jarFile. Then I used jarFile.jar instead of jarFile*.jar and then the problem got resolved
It can happen on a windows machine when you have spaces in the names of the folder. The solution would be to enter the path between " ".
For example:
java -jar c:\my folder\x.jar -->
java -jar "c:\my folder\x.jar"
To avoid any permission issues, try to run it as administrator. This worked for me on Win10.
I know this thread is years ago and issue was fixed too. But I hope this would helps someone else in future since I've encountered some similar issues while I tried to install Oracle WebLogic 12c and Oracle OFR in which its installer is in .jar format. For mine case, it was either didn't wrap the JDK directory in quotes or simply typo.
Run Command Prompt as administrator and execute the command in this format. Double check the sentence if there is typo.
"C:\Program Files\Java\jdk1.xxxxx\bin\java" -jar C:\Users\xxx\Downloads\xxx.jar
If it shows something like JRE 1.xxx is not a valid JDK Java Home, make sure the System variables for JAVA_HOME in Environment Variables is pointing to the correct JDK directory. JDK 1.8 or above is recommended (2018).
A useful thread here, you may refer it: Why its showing your JDK c:program files\java\jre7 is not a valid JDK while instaling weblogic server?
For me it happen because i run it with default java version (7) and not with compiled java version (8) used to create this jar.
So i used:
%Java8_64%\bin\java -jar myjar.jar
Instead of java 7 version:
java -jar myjar.jar
I had a similar problem where TextMate or something replaced the double quotes with the unicode double quotes.
Changing my SELENIUM_SERVER_JAR from the unicode double quotes to regular double quotes and that solved my problem.
this is because you are looking for the file in the wrong path
1. look for the path of the folder where you placed the file
2. change the directory cd in cmd use the right path
I use NetBeans and had the same issue. After I ran build and clean project my program was executable. The Java documentation says that the build/clean command is for rebuilding the project from scratch basically and removing any past compiles. I hope this helps. Also, I'd read the documentation. Oracle has NetBeans and Java learning trails. Very helpful. Good luck!
Maybe you have specified the wrong version of your jar.
I finally pasted my jar file into the same folder as my JDK so I didn't have to include the paths. I also had to open the command prompt as an admin.
Right click Command Prompt and "Run as administrator"
Navigate to the directory where you saved your jdk to
In the command prompt type: java.exe -jar <jar file name>.jar
Keep the file in same directory where you are extracting it. That worked for me.
This is permission issue, see if the directory is under your User.
That's why is working in another folder!
Rename the jar file and try
Explanation :
yes, I know there are many answers still I want to add one point here which I faced.
I built the jar and I moved it into the server where I deploy (This is the normal process)
here the file name which I moved already existed in the server, here the file will override obviously right. In this case, I faced this issue.
maybe at the time of overriding there can be a permission copy issue.
Hope this will help someone.
Have you tried to run it under administrator privoleges?
meaning, running the command in "Run As" and then select administrator with proper admin credentials
worked for me
I was trying this:
After giving the file read, write, execute priviledges:
chmod 777 java-repl.jar
alias jr="java -jar $HOME/Dev/java-repl/java-repl.jar"
Unable to access bla bla..., this was on Mac OS though
So I tried this:
alias jr="cd $HOME/Dev/java-repl/ && java -jar java-repl.jar"
This did not work "Unable to access jarfile"
"C:\Program Files\java\jdk-13+33-jre\bin\javaw.exe" -jar "C:\Program Files\Maxim Integrated Products\1-Wire Drivers x64\ OneWireViewer.jar"
This does work
"C:\Program Files\java\jdk-13+33-jre\bin\javaw.exe" -jar "C:\Program Files\Maxim Integrated Products\1-Wire Drivers x64\OneWireViewer.jar"
The difference is the single space in front of OneWireViewer.jar not withstanding that it is surrounded with quotes and even has other spaces.

How To Change An Association With A .jar File? [duplicate]

I just created a jar file using jdk7. I tried running it but kept getting an error. It turned out it was pointing at a jre6 and not the jre in jdk7. So, I used the following command to change it
ftype jarfile = "C:\path to jre\bin\javaw.exe" -jar "%1" %*
After that, I tried to double click on my jar file again, but it popped up a window asking me to choose a program to open it with. When I chose the same javaw.exe file as above it runs but exits immediately without doing anything. I can run it fine in cmd by doing the java -jar file.jar command. I even tried deleting the .jar registry key, but that just got me back to the point where it asked me to choose a program to open with. I'm running windows 7 ultimate if that matters.
EDIT:
In the registry there are 3 subkeys: OpenWithList, OpenWithProgids, and UserChoice. I've changed all the defaults to the path above except UserChoice which won't let me edit it from regedit. When I delete that and try to run the jar file, it opens up the choose a program to open with window again. For some reason it ignores the other registry keys
OK, I found the answer somewhere else but ran into this issue again so came here via google.
The solution is...
Open up regedit.exe
Either search for "jre6" or follow this: HKLM->SOFTWARE->Classes->jarfile->shell->open->command
Change the Data field to the directory of the javaw.exe file that's in your JDK directory.
For the love of God, don't forget to put the weird [-jar "%1" %*] parts on the end of it.
Enjoy outsmarting Oracle.
Hopefully this'll save some people a bit of time.
This happened to me when I installed the JRE6 plugin for Chrome, though it could happen when installing any other JRE I guess.
I fixed it by going to regedit and searched for jre which brought up...
HKEY_CURRENT_USER/Software/Classes/Applications/javaw.exe/shell/open/command
I changed the REG_SZ
from: "C:\Program Files (x86)\Java\jre7\bin\javaw.exe" "%1"
to: "C:\Program Files (x86)\Java\jre7\bin\javaw.exe" -jar "%1" %*
When windows asks you for a program to run it against it won't insert the needed -jar argument in the command line.
If it's asking you for the exe in which to run it then that means somehow your JRE isn't associated with the .jar extension. You'll need to modify the .jar file registry entry so that it runs c:\path_to_jre\bin\javaw -jar %1 in order for it to work.
It used to be under File Explorer > Tools > Folder Options > File Types.
Then look for .jar in there. You should find a text field that shows the executable to run and the command line arguments. That's what you want to modify. You can also do it in the registry editor too, but I've forgotten the HKEY variable path. I'm sure this KBA will help:
support_microsoft_kb_950505
Check if the file type jarfile is actually registered with the *.jar extension using
assoc .jar
Seems like .jar is not linked to jarfile.
I'm not sure exactly why none of the solutions worked, but I reinstalled jre7 and it works now.
Isn't it easier to change your java version to you rnew jdk7 by changing the CLASSPATH environment variable? See how here

javac and java commands through Windows batch files

I normally compile things through the command line using:
javac -classpath . Test.java
Similarly, I run them through:
java -classpath . Test
I'm now attempting to save myself the trouble of typing these out every time through batch files. I have attempted to do so through another question from here:
Creating a batch file, for simple javac and java command execution
I've also tried my own way:
cmd.exe
#echo off
javac -classpath . Test.java
Still no luck, however. I have checked that my PATH environment variable is correctly pointing to the latest version of jdk and as I've said, I can compile just fine directly through command line. Upon running the batch file, I just get the command prompt with no error; as if there was nothing under cmd.exe. Could anyone lend a helping hand and slap some sense into me?
When you write cmd.exe, that will start a new command prompt. You don't want that.
When you write #echo off, that means nothing will be printed on the screen after that point. That's what it means. That's what it does. That is why it looks like nothing is happening.
Something would be printed to the screen if you had a compilation error, but probably you don't.
If you want the command prompt window to stay around instead of disappearing, I believe there is an option in Windows to configure that, at least there was when I last used Windows, back in the mists of time.
this worked for me. I think it does what you were looking to do.
This is the code I suggest for the .bat file:
cd C:\Users\John\JavaApps\folderThatContains.java //points terminal to folder
javac Main.java //This compiles .java in said folder
cmd /K "java Main" //cmd /K prevents terminal from quitting after "java Main"

Making a "macro" command to run a program

I have a Main.java file and I want to run the program passing it test.txt
I know in command line I can write javac Main.java
After compiling I can write java Main test.txt and this will accomplish running the file and passing test.txt
If I wanted instead to be able to just write main test.txt and have that trigger my Main.class file to run is that possible and if so how?
(Edit: Based on your comment, let me expand to add a couple more situations)
If your goal is to have someone else run your program who does not have Java installed, and you do not wish to have them install a Java runtime environment before running your app, what you need is a program that converts the .class or .jar files into a native executable for the platform you are using. How to do this has been covered in other questions, eg: Compiling a java program into an executable . Essentially, you use a program like JCG (GNU Compiler for Java) or Excelsior JET (a commercial product) to expand the byte code into full native code with a mini-JRE built in.
If your goal is to save typing, there are a number of strategies. Others have suggested alias commands, which work well on linux.
A slightly more portable option that you could ship with your program would be a shell script. Granted, shell scripts only run on linux or other OS's with shell script interpreters installed.
Here is an example shell script. You paste this into a text editor and save it as main with no extensio. The $1 passes the parameter argument fyi.
#!/bin/sh
java Main $1
presuming you name your shell script just "main" with no extension, you could call main test.txt to execute your program now.
If you are on Windows, you might want to create a windows shortcut, and point the shortcut to "java Main test.text", using the full paths if necessary (if the paths are not already set). Of course, this does not make the parameter easy to change every time you run it, you would have to edit the shortcut.
add an alias
e.g. under a mac edit your .bash_profile with the following line
alias main='java main'
don't forget to open a new console to see your alias working
Depends on your operating system. On Linux with the bash shell, for instance, you can set up an alias to expand your main into java -cp myjar.jar main.
Linux can also be configured to 'understand' Java class flies as a binary format directly see here (linux kernel documentation).
If you're on windows, you'll have to wait for answer from someone with more knowledge about that than I.
Good luck!

Categories

Resources