At my company, we have users that manually type in code into command prompt line by line. I was trying to create something that would allow them to paste one line of code, but I get an error in the code when executing.
Here is my Code:
D: & cd b2borders & cd jar b2b & cd bin & set path=%path%;c:\Program Files (x86)\java\jre6\bin & java -jar ejecutable.jar & pause
Here is my error:
'java' is not recognized as an internal or external command, operable program or batch file.
Note: When I execute the code line by line, no errors.
Please advise!!!
Thanks,
Doug F.
As you are asking for a one line copy paste you could shorten your code thus:
CD /D "D:\B2Borders\Jar B2B\bin" & "%ProgramFiles(x86)%\java\jre6\bin\java.exe" -jar "ejecutable.jar" & Pause
You may also find that an alternative using Start works:
Start "" /D "D:\B2Borders\Jar B2B\bin" "%ProgramFiles(x86)%\java\jre6\bin\java.exe" -jar "ejecutable.jar" & Pause
To save your end users opening a Command Prompt themselves you could even try, (as entry into run box):
Cmd /K "Start "" /D "D:\B2Borders\Jar B2B\bin" "%ProgramFiles(x86)%\java\jre6\bin\java.exe" -jar "ejecutable.jar""
You are missing an s at C:\Program File**s**(x86)\java\jre6\bin in your screenshot.
But you seemed to have typed correctly in the question.
I'd suggest that as you are changing path in your cascaded command, the original value would be used in an attempt to locate java.exe, hence the error message.
The most obvious solution would be to establish a batch file to perform most of the retyping - manually entering that command would be error-prone, with the possibility of disastrous consequences if it is mistyped.
However, if you specify the entire executable name, "c:\Program Files (x86)\java\jre6\bin\java.exe" which would need to be within "double quotes as shown" because of the separators in the path, then the command should be executed properly. What java would think is yet another matter...
Related
Whenever I try and run mycommand.exe from my Windows cmd.exe terminal, I get this error:
''mycommand.exe' is not recognized as an internal or external command, operable program or batch file'
Secondly
I've also experienced a similar error when I tried to run C:\Program Files\My-App\Mobile.exe
''C:\Program' is not recognized as an internal or external command, operable program or batch file'
This is a very common question seen on Stackoverflow.
The important part here is not the command displayed in the error, but what the actual error tells you instead.
a Quick breakdown on why this error is received.
cmd.exe Being a terminal window relies on input and system Environment variables, in order to perform what you request it to do. it does NOT know the location of everything and it also does not know when to distinguish between commands or executable names which are separated by whitespace like space and tab or commands with whitespace as switch variables.
How do I fix this:
When Actual Command/executable fails
First we make sure, is the executable actually installed? If yes, continue with the rest, if not, install it first.
If you have any executable which you are attempting to run from cmd.exe then you need to tell cmd.exe where this file is located. There are 2 ways of doing this.
specify the full path to the file.
"C:\My_Files\mycommand.exe"
Add the location of the file to your environment Variables.
Goto:
------> Control Panel-> System-> Advanced System Settings->Environment Variables
In the System Variables Window, locate path and select edit
Now simply add your path to the end of the string, seperated by a semicolon ; as:
;C:\My_Files\
Save the changes and exit. You need to make sure that ANY cmd.exe windows you had open are then closed and re-opened to allow it to re-import the environment variables.
Now you should be able to run mycommand.exe from any path, within cmd.exe as the environment is aware of the path to it.
When C:\Program or Similar fails
This is a very simple error. Each string after a white space is seen as a different command in cmd.exe terminal, you simply have to enclose the entire path in double quotes in order for cmd.exe to see it as a single string, and not separate commands.
So to execute C:\Program Files\My-App\Mobile.exe simply run as:
"C:\Program Files\My-App\Mobile.exe"
When you want to run an executable file from the Command prompt, (cmd.exe), or a batch file, it will:
Search the current working directory for the executable file.
Search all locations specified in the %PATH% environment variable for the executable file.
If the file isn't found in either of those options you will need to either:
Specify the location of your executable.
Change the working directory to that which holds the executable.
Add the location to %PATH% by apending it, (recommended only with extreme caution).
You can see which locations are specified in %PATH% from the Command prompt, Echo %Path%.
Because of your reported error we can assume that Mobile.exe is not in the current directory or in a location specified within the %Path% variable, so you need to use 1., 2. or 3..
Examples for 1.
C:\directory_path_without_spaces\My-App\Mobile.exe
or:
"C:\directory path with spaces\My-App\Mobile.exe"
Alternatively you may try:
Start C:\directory_path_without_spaces\My-App\Mobile.exe
or
Start "" "C:\directory path with spaces\My-App\Mobile.exe"
Where "" is an empty title, (you can optionally add a string between those doublequotes).
Examples for 2.
CD /D C:\directory_path_without_spaces\My-App
Mobile.exe
or
CD /D "C:\directory path with spaces\My-App"
Mobile.exe
You could also use the /D option with Start to change the working directory for the executable to be run by the start command
Start /D C:\directory_path_without_spaces\My-App Mobile.exe
or
Start "" /D "C:\directory path with spaces\My-App" Mobile.exe
when you have this problem
search for Environment variables
press on Environment variables
Under "System varibles" you search for PATH and press edit
press on new and add the path of your program and save it
I'm using Windows, and my _vimrc file has the following lines:
autocmd Filetype java set makeprg=javac\ -d\ %:~:h:s?src?bin?\ %
set errorformat=%A%f:%l:\ %m,%-Z%p^,%-C%.%#
map <F9> :make<Return>:copen<Return>
map <F10> :cprevious<Return>
map <F11> :cnext<Return>
map <F12> :!start cmd /k "java %:~:s?src?bin?:r"
I just want to use this for quick editing, and I will be using an IDE most of the time, thus the need for the classic /src/ and /bin/ folders (they are at the same folder level).
So it happens like this:
- I open the .java file that I want to edit in vim (it is in the /src/ folder)
- I hit F9 and javac runs on my current file (%) and puts the class files in the /bin/ folder (this seems to work as expected), and a new error window is opened in vim to display compile errors
- I hit F10/F11 to cycle between errors
- I hit F12 to open a command prompt and execute the java program (I think I have to open the command prompt because my program asks for user input in the console), except it gives an error in the command prompt that it could not find or load the main class
I don't know why it won't work, the class paths seem correct. Can you show me where I have went wrong?
Edit: #merlin2011 led me to the following answer:
map <F12> :!start cmd /k "cd %:~:h:s?src?bin? & java %:r"
I had to cd to /bin/ and then run java.
Or: without changing directory, and just changing the classpath:
map <F12> :!start cmd /k "java -classpath %:~:h:s?src?bin? %:r"
For permanency, I am converting my comment into an answer.
In order run the Java command from vim, one must first cd to the directory bin and run the Java command from there.
map <F12> :!start cmd /k "cd %:~:h:s?src?bin? & java %:r"
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"
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)
IS there any way to start a blank console in Windows platform ?
I'm writing a CLI where i want to open a separate window where user can log in and write their own command. When executing with cmd /c start command, it starts windows standard console.
Is there any other command ???
Assuming you're trying to start a java jar file, use a command like this:
start /d "%~dp0" java -jar "%~dp0\fpa.jar"
%~dp0 expands to the drive letter and path in which the batch file is located. Use that if you want to want to make sure that the PWD when running is the same location as the batch file. Otherwise, juse use
start java -jar "%~dp0\fpa.jar"
This will make sure that the batch file works even if you run it when not in the same directory as the jar file, as long as the jar file is in the same directory as the batch file.
You may need to make sure that java is in your path by having a line like
set path=jre6\bin;%PATH%
Also, you can eliminate the command line windows that comes up (for a GUI program by example) by using javaw instead of java.
You can use batch file in windows which would in turn start application that would accept user input. Something similar to this :
start cmd /c myapp.bat