How to Download Java and run a file on command line - java

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.

Related

Why is the java PATH variable needed only for terminal execution?

Why is there no need to add the path of the java installation to the PATH variable when starting a jar with double-click while it is needed to start the jar from command line? Why does the OS only know where java is installed when executing with a double-click (without the PATH variable)?
Related Questions:
Is it necessary to set the path variable in java installation?
Is JAVA_HOME variable needed when Java Path is defined in system environment variable?
When on the command line, you call an executable and pass it a file as an argument. Since it does not know where this executable is, you either need to call it with an absolute path (Includes the entire path from the drive letter to the executable) or the executable must be present in one of the locations listed in the system PATH environment variable.
However, when double clicking a file Windows does not have the luxury of being told which application it should use ahead of time. To solve this issue, Windows keeps a global registry of programs, file extensions, and various metadata about them. This includes stuff like which icon to display for that file type, the path to the executable used to open that file, extensions associated with a given program, the path to the uninstaller to use if deleted from the control panel, and much more.
The Java installer handles updating the registry for you behind the scenes so you don't need to, however depending on your installation setting it may not update the system path since most people will only use the double clicking method.

Ionic build failed; invalid %JAVA_HOME% (android)

I've searched around a bunch and could not find an answer that could help me.
I get this message when building my Ionic project for Android. I have verified, that both java -version and javac work in the folder, so both are accessible via the %path%.
What can I do to resolve this, and did I miss something?
I'm not familiar with Ionic but apparently it is not using %PATH% , it is using %JAVA_HOME%. You need to set it.
Determine the directory of a Java JDK (not JRE) in your system. It could be something like C:\Program Files\Java\jdk1.8.0_65. Since javac responds, you must have a JDK in the path somewhere. This directory will be the value for JAVA_HOME. (NOTE: This is not the same as the Java bin directory in the path! There should not be a bin on the end of the directory.)
Open the System page in your Control Panel.
You can do this in various ways:
Open an Explorer window, right-click on This PC and click on Properties in the menu.
Open Control Panel, click on System and Security, click on System.
Click on Advanced System Settings.
The System Properties window opens. Select the Advanced tab.
Click on the Environment Variables... button.
The Environment Variables window opens. Look at the bottom section labeled System Variables. You should see a variable named JAVA_HOME in the list. If you have this variable, double-check that the directory in the value exists and is a valid JDK directory.
If you don't see JAVA_HOME in the list, click the New... button. If JAVA_HOME is there but it's the wrong value, select JAVA_HOME in the list and click the Edit... button.
The New System Variable or Edit System Variable window will open (they look about the same.) Enter "JAVA_HOME" for variable name and the JDK directory for variable value. You can use the Browse Directory... button to make sure you have a valid directory for the value.
Click the Okay button on the New/Edit System Variable window, then the Environment Variables window, and then the System Properties window.
You will need to log out and log in again for the JAVA_HOME variable to take effect.

Run a .jar file on Windows 7

I have downloaded a .jar file and it won't run on my computer. I have installed Java RE 5.0 and have Java 7 update 72. Every time I double click on the file it pops up and closes.
I have reinstalled the java re and java 7u72 and still no success.
Any help with running executable .jar file on windows 7?
By default, java does not run in windows mode, maybe it's just a console java program.
The .jar file must have a lib folder in the folder the .jar is in to open a window (without using native java libraries). If it doesn't, then the .jar won't open any windows, as it needs external libraries to do so.
However, if your .jar isn't meant to open any windows, then it has been performing as expected. Try running your .jar from the command line:
java -jar <jar-file-name>.jar
Have you checked the Environment Variables? Maybe the variable PATH is not specified. Besides, you can try this:
Start - "Control Panel"
Click on "Default Programs"
Click on "Associate a file type or protocol with a specific program"
Double click on the .jar
Browse C:\Program Files\Java\jre7\bin\javaw.exe
Click on the button Open
Click on the button OK
If it doesn't work, try to configure the Environment Variables:
From the desktop, right click the Computer icon.
Choose Properties from the context menu.
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.
Note: You may see a PATH environment variable similar to the following when editing it from the Control Panel:
%JAVA_HOME%\bin;%SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem
Variables enclosed in percentage signs (%) are existing environment variables. If one of these variables is listed in the Environment Variables window from the Control Panel (such as JAVA_HOME), then you can edit its value. If it does not appear, then it is a special environment variable that the operating system has defined. For example, SystemRoot is the location of the Microsoft Windows system folder. To obtain the value of a environment variable, enter the following at a command prompt. (This example obtains the value of the SystemRoot environment variable):
echo %SystemRoot%
More info here
Hope it helps.
Clemencio Morales Lucas.

java compiler environment not working windows

Hi I have set the environment variable for java in windows.
when I type java in cmd, it works,but when I type javac ,it doesn't work saying javac' is not recognized as an internal or external command,
Doing Path="C:\Program Files\Java\jdk1.7.0_09\bin works but when I close the command prompt and reopen again same error.
Is there anyway out to set this permanently.
You can add C:\Program Files\Java\jdk1.7.0_09\bin to your path using the System Control Panel in Windows.
Go to Control Panel -> System -> Advanced System Settings -> Environment Variables...
Find Path, and edit it to add your jdk path.
It sounds like you simply setting the PATH variable of the current session. What you need to do it go to "Computer/My Computer", right click and select "Properties". From there click "Advanced System Settings" and then the "Environment Variables button. Navigate to the PATH variable and append to the end of the string. Directory paths must be separated by a semi-colon.
All of the variables in this section are loaded each time a new process is launched. You can also add a default CLASSPATH variable here and include all of your go-to jars.
Recently I have faced this problem and my finding is for Windows 7 adding javac to the "path" environment variable works but for windows 10, you can do anything means Add a new environment variable or update the existing one.

setting class path in Java

I used to run eclipse on my laptop. I have installed and used Secunia PSI application in order to update the apps, but once I done that I cannot run Eclipse.
This is what I have:
I set the PATH to the directory which contains javaw.exe app. The path at the PATH variable is the same as of CLASSPATH variable.
So, what are your resolutions? Done, it works now. I just simply needed to add '.;' at the end of the path link at the PATH.
Cheers
As the error message clearly states, Eclipse cannot find the entire JVM.
Add the directory that contains javaw.exe to your PATH.
Better to add JAVA_HOME as a new system variable in the same screen where you see the class path by clicking New and providing the folder location for JAVA root installation folder e.g. below:
Variable Name= JAVA_HOME
Variable Value= C:\Program Files\Java\jdk1.7.0_09

Categories