Javac isn't working in windows command prompt - java

javac not working in windows command prompt
^I tried the suggestions on this post, and it was SLIGHTLY helpful, but not completely.
I opened up my command prompt and I typed in "javac" after putting in the path in my Environment Variables and it didn't work, at which point I googled it and found that thread.
I knew that I had closed and re-opened my cmd already, and that didn't work, so I skipped that bit and I saw the part telling me to make sure that javac.exe exists, which I verified with the "dir" command in the cmd. Afterwords, while in the "C:\Program Files\Java\jdk1.7.0_25\bin" folder on the command prompt, I typed in the next bit of advice, which was
for %i in (javac.exe) do #echo %~$PATH:i
After entering this into my command prompt, I got the message "ECHO is on". Upon seeing this, I typed in "javac" again and this time, it worked. So I decided to test this out by backing out of the directory and going to a folder in which I had a .java file saved and running it, but it again told me that
'javac' is not recognized as an internal or external command, operable program or batch file.
This was disappointing. I think it'll only work if I'm INSIDE the bin file on the command prompt, which is annoying because I'm not an administrator on this computer and it will be annoying to always have to get admin permission (from my parents) to code. They will also never give me the password. Can anyone help me? Thanks in advance! And sorry for the huge wall of text...
EDIT: Someone has asked what would the output of "echo %path%" be. It is this:
C:\Windows\system32;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\
^Is it not supposed to say this?
EDIT 2: #brano88...I think I did? I right clicked computer, went to properties, went to advanced system settings, clicked Environment Variables and went to the top part and pressed "New..." There, I entered the Variable Name as Path and the Variable Value as the location of the bin folder. Is this incorrect? I followed a YouTube tutorial step by step while doing this.
This one: http://www.youtube.com/watch?v=Hl-zzrqQoSE

How to run .java files from CMD
go to your Computer -> C: -> Program Files -> Java -> jdk1.7.0_25-bin
copy the path (example: C:\Program Files (x86)\Java\jdk1.7.0_25\bin)
Go to Control Panel -> System and Security -> System-Advance System
Settings -> Advanced -> Environment Variables
open the Environment Variables screen and go to System Variables and look for "Path"
after finding the Path system variable, double click it or press edit button and in the Variable value you paste the path from java you just copied after the last values already existing there.
Note!
make sure you DO NOT enter any extra space in this field as it won't work;
make sure you have one semi-colon before pasting the path, example: Path :
...%ANT_HOME%\bin;C:\Program Files (x86)\Java\jdk1.7.0_25\bin
Note! If you previously tried to compile the .java file in a CMD, close that CMD
and open it again as the changes made will take effect only using a new instance of CMD
Go to the location of the file.java , example:E:\Projects , right-click by holding the Shift button pressed inside your folder and in the options from the window that just appeared select: Open command window here
Another solution is to normally open a CMD and change the directory using :
cd command until you reach your folder
After the CMD window opened, type: javac HelloWorld.java
Note! Make sure the class name written in your file.java is the same as the file name. Example your file name should be: HelloWorld.java and your class inside that file must also be:
class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello World!"); // Display the string.
}
}
Otherwise, it will not compile!
It is successful if no error message is displayed, if the path of the folder appears again in CMD and of course if the HelloWorld.class file appears now in the directory. To check that type the command dir which will show what contains your currect directory.
To finally run the file type in the CMD: java HelloWorld
Observe that no extension is needed when running the file(the file you run already has the .class extension)
This is how it worked for me! If something is not right, please inform me! Thanks!

For a beginner programmer that doesn't have admin rights on his/her computer, I'd recommend the Eclipse IDE.
Since you already have the JDK the only installation step needed requires no admin rights. From here you must download the "Eclipse Standard" option, and you will get a very large zip archive. You can extract it onto the desktop or my documents. Windows comes with a utility to do this via drag-and-drop right from the explorer or your machine may have another program such as WinRAR installed to do this.
You can then run eclipse.exe from the place where you extracted it by browsing to, and double-clicking this file.
The IDE is very powerful and self-explanatory. You can create projects, run, and debug code, and it's nice for beginners. It's truly worth the long wait in downloading it.

First, yes you did add JDK to PATH. But you didn't do that correctly. You already have these variables added to PATH: C:\Windows\system32;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\
To add JDK put semicolon before you add it because you have multiple path's assigned to PATH variable. So it should be something like this:
C:\Windows\system32;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files\Java\jdk1.7.0_25\bin

in my case, javac was not working just because while specifying the path to javac in the path variable I gave space after putting semicolon to the end of the previous entry

To use javac from command prompt without typing the full location each time you will need to add it to the path.
I am unsure if you can change it without admin privileges, But on windows 7 with admin you can go to start -> Right click My Computer -> Properties -> Advanced system settings -> Advanced -> Enviromental Variables
You can then find the system variable path and append to the end of it, the location of javac.
The exact location of javac will vary depending on what version of the JDK you have installed, During installation you will have been given the option to choose where it was installed.
EDIT: Also make sure you haven't opened cmd as administrator. Or perform the steps in that video on the administrator account.
Adding variables to the top part of the environmental variables menu, mean they only affect the current user. You should be able to do these on normal account if you have someone type in the administrator password.

Related

How to Download Java and run a file on command line

I know this is SUPER basic. I am very new to all of this. I tried to download java and run a helloworld by following these instructions: https://docs.oracle.com/javase/tutorial/getStarted/cupojava/win32.html
I got an error when I tried to use the "javac" command to compile. This is what appears in the command prompt:
C:\Users\USer18\Desktop>javac HelloWorldApp.java
'javac' is not recognized as an internal or external command, operable program or batch file.
Does this mean I downloaded java incorrectly? When I downloaded it, there were 3 different things to choose from, but I could only choose one, so I chose the first one. I tried to download java again and select the second one, but it said it didn't work.
Thanks in advance for helping me!
From the tutorial you linked, it tells you to "consult the installation instructions" found here: https://docs.oracle.com/javase/8/docs/technotes/guides/install/install_overview.html
For Windows JDK, the instructions linked are here: https://docs.oracle.com/javase/8/docs/technotes/guides/install/windows_jdk_install.html#CHDEBCCJ
The part you need to look for is "Updating the PATH Environment Variable"
Updating the PATH Environment Variable
If you do not set the PATH variable, you need to specify the full path
to the executable file every time you run it, such as:
C:> "C:\Program Files\Java\jdk1.8.0\bin\javac" MyClass.java
It is useful to set the PATH variable permanently so it will persist
after rebooting.
To set the PATH variable permanently, add the full path of the
jdk1.8.0\bin directory to the PATH variable. Typically, this full path
looks something like C:\Program Files\Java\jdk1.8.0\bin. Set the PATH
variable as follows on Microsoft Windows:
Click Start, then Control Panel, then System.
Click Advanced, then Environment Variables.
Add the location of the bin folder of the JDK installation to the PATH variable in System Variables. The following is a typical value
for the PATH variable:
C:\WINDOWS\system32;C:\WINDOWS;C:\Program Files\Java\jdk1.8.0\bin
Note:
The PATH environment variable is a series of directories separated by semicolons (;) and is not case-sensitive. Microsoft Windows looks
for programs in the PATH directories in order, from left to right.
You should only have one bin directory for a JDK in the path at a time. Those following the first instance are ignored.
If you are not sure where to add the JDK path, append it.
The new path takes effect in each new command window you open after setting the PATH variable.
When ever we execute any command, it is searched in the directory where we are current in or mentioned in PATH environment variable. The oly thing which you need is just add <path of yourjdk>\bin to PATH
Windows 10 and Windows 8
In Search, search for and then select: System (Control Panel)
Click the Advanced system settings link.
Click Environment Variables. In the section System Variables, find the PATH environment variable and select it. Click Edit. If the PATH environment variable does not exist, click New.
In the Edit System Variable (or New System Variable) window, specify the value of the PATH environment variable. Click OK. Close all remaining windows by clicking OK.
In the Edit System Variable (or New System Variable) window, specify the value of the PATH environment variable. Click OK. Close all remaining windows by clicking OK.
Windows 7
From the desktop, right click the Computer icon.
Choose Properties from the context menu.
Click the Advanced system settings link.
Click the Advanced system settings link.
In the Edit System Variable (or New System Variable) window, specify the value of the PATH environment variable. Click OK. Close all remaining windows by clicking OK.
Reopen Command prompt window, and run your java code.

I could not understand why "javac: file not found: HelloWorld.java"

javac HelloWorld.java
Following error message keeps coming out every time when I run in cmd
javac: file not found: HelloWorld.java
However, when I added my file location
javac D:\User\Documents\Project\HelloWorld.java
It runs successfully.
Why?
I have tried to edit variable value of PATH to D:\User\Documents\Project
But it did not work.
You seem to have never used the command prompt before. The command prompt has a "current directory", just like in a file explorer window, there is a "current directory", the directory that you are viewing right now.
The current directory is usually shown just to the left of the caret.
The current directory on the above photo is C:\Users\Mrhope.
Type dir to see all the files and directories of the current directory. You can then use cd to navigate to a subdirectory. Say you are in Documents now, and you can do this
cd Project
to go to the Project folder. By the way ../ means the parent directory
The HelloWorld.java in javac HelloWorld.java is a path relative to the current directory, so this will only work if HelloWorld.java is in the current directory. You should try to use cd to navigate to the correct directory then use that command.
Setting the PATH environmental variable is quite irrelevant here because that is setting the path of the JRE. Please set it back.
I suggest you learn the basics of cmd first. Many tutorials are available online.
You have to enter the directory of where your Java file is.
Fastest way is to open the folder where your Jar file is then hold the shift key on your PC and right click on an empty space in the folder (while still holding the shift key). When the context menu comes up look for the open command window here option (for newer Windows 10 versions it's power shell window), select the appropriate option. When it opens run your command

'javac' is not recognized as an internal or external command, operable program or batch file. in VS Code, using Code Runner extension

I am trying to run some java code in VS Code with the Code Runner extension, but i keep getting this:
'javac' is not recognized as an internal or external command,
operable program or batch file.
I checked all the paths and updated the path in VS Code, but it did nothing.
Assume you are on the Windows System.
First, you might want to add your jdk path to window system environment.
Then, open your VS Code, and go to User Settings located under File -> Preferences -> User Settings.
Add jdk PATH in your VS Code as the following shows.
Important Step: after all above steps are done, you might want to restart the VS Code to let change go in effect.
To test if it works, open Integrated Terminal in VS Code under View (or type Ctrl + ` (this key is located next to number 1)
Once the terminal shows up and is initialized, type javac to verify VS Code recognize the command.
[On Windows]
Although it is called "bin path", the "bin" folder is not supposed to be included in the path. If you do, you get an error and VSCode asks you to remove "bin" from the path in order to solve the issue.
So the path to be added in settings.json should be something like:
"C:/Program Files/YOUR JDK/(YOUR JDK VERSION)-hotspot".
(and not "...-hotspot/bin")
Try and add the jdk bin path to the system environment variable otherwise u need to save the Java code inside the bin folder and compile it from there.

Java Language----"file.java.swp" instead of "file.java", resulting javac to be not recognized?

I am using the Java language and trying to program for a simple single line of text.
I turned on DOS and got to make a file (with a "vi thing" I downloaded with Cygwin) like this:
C:\folder>vi Howdy.java
I press enter and a blank space appears and I type EXACTLY like in the tutorial video:
apublic class Howdy {
public static void main(String Arg[]) {
System.out.println("Howdy Y'all!);
}
}
Then compile the program. I turned on another DOS, and got it to display my files and directories. In the tutorial video, the instructor's file appeared correctly as "Howdy.java", but mine appeared as "Howdy.java.swp".
When I tried to use javac with it, DOS told me javac was not recognized as an internal or external command.
I am very perplexed and deeply troubled! I've been trying to troubleshoot it all day long but couldn't get it. If anyone would shed some light at all, I would be tearfully thankful.
It seems like you don't have the Java compiler(javac) in your PATH.
Find your Java JDK install directory (it's usually C:\Program Files\Java\jdk1.6.0 or something similar). Then, follow these instructions on how to set your PATH. Make sure you put the bin directory on the path (it's the one with the Java executable files, like java and javac). Once you're done with that, close any open command prompts and re-open them.
That .swp file is the vim temp file that is created when you have the file open. You should have the non .swp file in the same directory. Try removing the .swp part when you compile. It's there for file integrity purposes. You can ignore it for the most part.
Go to the directory where javac is installed and run it. Seems you did not set JAVA_HOME variable

no output when .jar is executed

I built an application in Netbeans 6.8 and made project.jar file. When I run it, it works only on my computer, but not on any other computer. However, when I made any simple application, that doesnt use any libraries, it works fine on any computer.
Is there any way, how to invoke some error message, where is the problem?
My project use R 2.9.2, so I install this version on other computer and set the System Path variable exactly same. Other libraries listed in lib directory are: AbsoluteLayout.jar,DatePicker-V0.99-2006.09.01.jar,jcommon-1.0.16.jar,jfreechart-1.0.13.jar,jmathplot.jar,JRI.jar,pdf-renderer-1.0.5.jar
Thank you
You don't get any message at all? What do "works" and "not works" look like?
You sound like another person who hasn't taken the time to learn how to do things by hand on the command line without an IDE. I'd recommend doing that. Open a command shell and type in the java -jar -cp ... foo.jar command to run your stuff. The messages you get back will be educational.
Note the -cp command line argument. That's how you add your JARs to the CLASSPATH properly.
I solved this problem as follows, maybe it will help someone.I add 2 paths in PATH system variable:
Start -> Control Panel -> System -> Advanced
Click on Environment Variables, under System Variables, find PATH, and click on it.
In the Edit windows, modify PATH by adding the location of the class to the value for PATH.
you must add both paths, to jri.dll and r.dll, in my case it were these:
C:/Program Files/R/R-2.9.2/bin/;C:/Program Files/R/R-2.9.2/library/rJava/jri/;
I have added these lines already, but with different different slash. So be careful, you must use it / not \ to define path!!!

Categories