javac command prompt parameters - java

I installed java on my machine and I wanted to compile this group of source code. But when I opened up the command prompt and typed in javac, the command prompt said it was an unknown parameter. The same thing happened when I wanted to build something with ant (I had the build.xml if you are wondering) and the same thing happened. How can I set the javac and ant command to be used in any directory?

First of all you have to be sure you installed JDK and not only JRE
Secondly it depends on your operating system:
windows: you have to reach environment variables by using My Computer (manage->advanced->something here)
linux/osx: you have to export variables with export command or by using a .profile file in user directory
Then you need to set two things:
adding /jdk_1.6.../bin/ to your PATH variable
setting JAVA_HOME to your root of java installation ( /java_1.6..../ ) (maybe this is not required by java itself but many frameworks use it)
One note: on OSX, and maybe Linux too you have to split paths in PATH variable with double colon ':' instead that semicolon ';'

Add the directory the executable is in to the PATH environment variable.

Guessing that you are using Windows. Right click My Computer and click Manage. Then, in the Advanced tab, select Environment Variables. In the new window, under System variables, find Path and append the full path to the binaries (javac and ant) at the end, separated by semicolons.

Related

How does Windows OS decides which JDK compiler to use(oracle JDK /GraalVM jdk), When multiple JDK's are present on 1 system?

I have Oracle JDK 17 installed on my system at the default location in my Windows 10 OS.
I can use the javac compiler from the command line.
I am going to install GraalVM JDK [java17] in my system (zip file) at a different location.
Online docs have instructed me to add the path to GraalVM bin directory (Eg C:\path\graalvm\bin) to the PATH variable on Windows .
Now when i type C:\javac mySourceFile.java and press enter
How will the OS decide which java compiler(oracle javac /GraalVM javac) will it use to compile the source code as both of them are named javac ?
Here i am not explicitly passing the location of the javac to be used.
is there any way to specify which jdk to use at command line?
Please note that compiling is done using command line tools only and no IDE is used.
(I am aware that IDE's allow you to select the JDK based on the location using preferences/settings)
When you type "javac MyClass.java", the path list defined in the PATH variable will be searched from left to right. When it finds a match, it will use that command. So, it's possible you may have include multiple java\bin folders in the path. It will execute the first one it finds. If you want a particular version of Java to execute, add it as the first path to the path variable.

How to get java command running?

When working with any command line (windows-cmd, git bash, ..), no Java commands are working.
I executed java -version and nothing happens. I can type again.
I have set my JAVA_HOME variable to C:/Users/myName/Java/jdk1.8
and I have also added %JAVA_HOME%\bin to my Path variable.
It's not, that the command does not get recognised. It's just that nothing happens.
I could probably run the java.exe command directly, but I wanna fix the env variable.
Can someone help?
Best regards
Try this (Windows):
Locate your JDK/JRE binary - Usually the path for your JDK/JRE bin (On Windows) is:
Your OS Drive(C or D etc):\Program Files\Java\jdk[version]\bin. After you've located it, Copy your path to your clipboard.
Search for "Environment Variables" on windows.
Create a new User variable name it (usually named "JAVA_HOME") and copy your path as a value.
Now in System variables locate a variable named "Path" and add the same value set in JAVA_HOME.
Close and then reopen the CMD.
If this solution does not work, reinstall java. You can uninstall through:
Control Panel -> Programs -> Under Programs and Features -> Uninstall a program.

Run a jar file only with jre [duplicate]

When trying to check the current version of Java in which I am running, I received the error "java is not recognized as an internal or external command, operable program or batch file.".
I am running Windows 7 OS and have downloaded the latest JDK and felt I may have accidentally deleted the java from machine as before I was able to check the Java version using the command "java -version".
What software must I download to get Java working on my machine again?
EDIT:
I have managed to get Java running from my cmd again after ensuring all environment variables pointed to the current Java SDK.
You need to configure your environment variables, JAVA_HOME and PATH.
JAVA_HOME must contain the path to java, and you should add %JAVA_HOME%\bin to PATH
Alternatively, you can simply add to your PATH the whole path to the bin folder, without the JAVA_HOME variable, however, this makes a little more annoying when you need to have more than one java version on your machine (that way you only need to change JAVA_HOME and don't even bother with PATH)
For Windows 7:
Right click on My Computer
Select Properties
Select Advanced System Settings
Select the Advanced tab
Select Environment Variables
Select Path under System Variables
Click on the Edit button
In Variable value editor paste this at the start of the line
C:\Program Files\Java\jdk1.7.0_72\bin;
Click Ok then Ok again
Restart command prompt otherwise it won't see the change to the path variable
Type java -version in the command prompt.
Notes on Step 8:
The version of java in this may be different from the one used here -- this is only an example.
There will probably be other values in the path variable. It is really important that you don't delete what's already there. That's why the instructions say to paste the given value at the start of the line -- this means that you don't remove the existing value, you just put java before it. This also fixes any problems you'd be getting if an other version of java is also on the path.
Notes on Step 6:
This sets the path for the computer, not for the individual user. It may be that you're working on a computer which other developers also use, in which case you'd rather set the user variables, rather than the system variables
It sounds like you haven't added the right directory to your path.
First find out which directory you've installed Java in. For example, on my box it's in C:\Program Files\java\jdk1.7.0_111. Once you've found it, try running it directly. For example:
c:\> "c:\Program Files\java\jdk1.7.0_11\bin\java" -version
Once you've definitely got the right version, add the bin directory to your PATH environment variable.
Note that you don't need a JAVA_HOME environment variable, and haven't for some time. Some tools may use it - and if you're using one of those, then sure, set it - but if you're just using (say) Eclipse and the command-line java/javac tools, you're fine without it.
1 Yes, this has reminded me that I need to update...
Assume, Java/JDK is installed to the folder: C:\Program Files\Java:
Follow the steps:
Goto Control Panel → System → Advanced system settings → Advanced → Environment variables (Win+Pause/Break for System in Control Panel)
In the System variables section click on New…
In Variable name write: JAVA_HOME
In Variable value write: C:\Program Files\Java\bin, press OK:
In the System variables section double click on Path
Press New and write C:\Program Files\Java\bin, press OK:
In Environment variables window press OK
Restart/Run cmd.exe and write: java --version:
Search environment variables.
open the "edit the system environment variables".
then click on "environment variables".
Under "User variables" click on "Path" then "Edit".
Find your Java path and click "Edit".
then paste the path of your java installation folder.
Mostly you can find it on a path similar to this.
C:\Program Files\Java\jdk-12.0.2\bin
Then click OK.
now in the start menu, type cmd.
open the command prompt.
type
java -version
If you did it right,it should show something like this.
For me its start working after putting ,: in the starting of the system variable path :--
My solution was to put same value (path to JDK bin folder) in JAVA_HOME and Path
In my case, PATH was properly SET but PATHEXT has been cleared by me by mistake with .exe extension. That why window can't find java or anything .exe application from command prompt. Hope it can help someone.
This problem is on Windows 8.
First copy your Path of java jdk - e.g. C:\Program Files\Java\jdk1.7.0_51\bin.
Right on the My Computer Icon on the Desktop and Click Properties.
Select 'Advanced System Settings' in the left pane.
Under 'Advanced' tab, select 'Environment Variables' at the bottom.
In System Variables, select 'Path' Variable and edit it.
Paste the path and add a ';' at the end - e.g. C:\Program Files\Java\jdk1.7.0_51\bin;
I had the same problem. Just Install the exact bit of java as of your computer. If your PC is 64 bit then install 64 bit java. If it is 32 bit then vice versa :)
Not sure why, but in my case, the reason was because I was running Anaconda terminal instead of the CMD.
After I use CMD and update the path settings as mentioned by all comments above the issue solved on my side.
In case you are using a laptop and do not have the Pause\Break button.
For windows 10 users with 20h2 and above:
1 WIN → type "About your PC" → scroll at the bottom → Advanced system settings → Environment variables or WIN+R → shell:::{bb06c0e4-d293-4f75-8a90-cb05b6477eee} to open Classic System Properties → on the left side → Advanced system settings → Environment variables
Also for Windows 10 and Windows 7:
1 WIN → This PC → properties → Advanced system settings → Environment variables
In the System variables section click on New…
In Variable name write: JAVA_HOME
In Variable value write: C:\Program Files\Java\jdk-15.0.2\bin, press OK
I have taken steps 2-4 from Vijay Bhatt
There is a bit faster way to set a system variable. Run a console (terminal) as an administrator.
General command synax to add a new variable:
setx variableName value /M
In our example, we would need to set it as
setx JAVA_HOME "C:\Program Files\Java\jdk-15.0.2\bin" /M`
Like this:
/M - flag specifies to set the variable in the system environment.
After command execution, you should see the message:
SUCCESS: Specified value was saved.
How can I check that it is added?
Close your active terminal;
Open your favorite terminal;
Type java -version.
You should see something similar to this:
Notice:
User variables can be created w/o having a root (administrator privileges), whereas to create System variable; You need to open a console as a root.
I have used C:\Program Files\Java\jdk-15.0.2\bin as an example, in your case, it could be different from mine.
I opened a new command prompt in Windows 10 after updating the environment variables without closing the old one(To have my commands handy and lazy to type again)
Still, the new cmd window was referring to the previous version of Java.
Then once I closed the all cmd prompts that ran with admin privileges, the new java version was getting reflected.
I corrected my path variable but command prompt need to Restart otherwise, it won't be able to verify the change to the path variable. May be helpful for someone like me. so
"restart command prompt"
Restart the command prompt before checking the version of JDK installed. I spent 02 days on it until my problem resolved when I restarted the command prompt before checking javac - version, javac etc.
if you have cygwin installed in the Windows Box, or using UNIX Shell then
Issue bash#which java
This will tell you whether java is in your classpath or NOT.
If you have set the environment variables (JAVA_HOME and PATH) under user variables, command prompt (run as administrator) will not identify java. For that you need to set environment variables under system variables.
Try this:
System variables:
PATH = /bin folder of your jdk install
https://docs.oracle.com/javase/10/install/installation-jdk-and-jre-microsoft-windows-platforms.htm
Just some extra information for people that have still problems,
instead of editing the path variable of java, delete that part of the path(only the java!!)
and make a new variable pointing tpo the jdk/jre.
this seemed to work for me.

How do I add `java` to the command path on Vista?

I have JDK installed in Vista. I want to run java from the command line. How do I configure the path variable?
If I'm not able to set the JAVA_HOME variable, what is an alternative?
Open My Conputer -> General Settings -> Environment variables and add as global
Key:JAVA_HOME
Value:C:\jsdk etc. etc.
And then modify the Path variable
Value ( type at the end )
....something;something;something;%JAVA_HOME%\bin
Reopen your command prompt and type
javac
And see if it works
EDIT
Moved my comment to the answer
The command prompt has some problems dealing with the empty space.
What I usually do is install the jdk directly on C: to get rid of space in "Program Files"
So installing it directly in C: will give you an "spaceless" path to the executable:
C:\jdk1.6.0_14\bin\javac
Instead of
C:\Program Files\Java\jdk1.6.0_14\bin\javac
Some times you'll get: C:\Program "executable not found" or something like that.
Installing directly on C: does the trick
I'd suggest looking at the java tutorial.
http://java.sun.com/docs/books/tutorial/essential/environment/paths.html
CLASSPATH manual
Running java from the command line
If the java program is available in your path then you should just be able to do;
c:> java -classpath somedir com.example.Main
if java isn't available to you then you can do
c:> SET JAVA_HOME=c:\path\to\java
c:> <same java stuff from above>
Use the -classpath option for the java command to specify where your classes are.
The value of the classpath option is a list of directories, JAR archives, and ZIP archives that contain classes, structured into directories that mirror the classes' package structure. The list uses the path delimiter, which is plaform specific: on Windows, it is ";", other platforms use ":".
Using the CLASSPATH environment variable is discouraged because changes for one program are likely to interfere with the operation of other programs.
There is some problem with Vista and the path variable containing %JAVA_PATH% if the JAVA_PATH value has spaces.
I fixed this manually by setting the %JAVA_PATH% to c:\program files\Java\<whatever> and then added to c:\program files\Java\<whatever>\bin to the Path variable in Vista.
If you deleted your My Computer icon, you can also go to Control Panel -> System -> Advanced system settings (which gives you System Properties dialog) -> Advanced tab -> Environment Variables. Path is listed in the second text window.

javac not recognized

What can I do when I keep receiving the error
'javac' is not recognized as an internal or external command, operable program or batch file
when I want to compile my jar or .class file?
Thanks
Make sure %JAVA_HOME%/bin is on your %PATH% (or $JAVA_HOME on the $PATH in *nix).
It means that it is not in your path. You have the following options:
1) Change to the directory where javac lives before calling it.
2) Use the full path to javac when making the call, e.g. C:\java\jdk1.6.0_12\bin\javac ...
3) Add the javac directory to the PATH environment variable
This just means that javac isn't in your PATH variable.
On Windows, you just have to add the folder that contains javac.exe to the PATH environment variable (Win+Pause/Break).
On Linux/Mac/Unix, just append that directory to $PATH in .bashrc or similar.
The error that you are seeing is generated by Windows, and isn't specific to Java. That error means that you typed a command and Windows could not find the command by that name.
The easiest way of rectifying the situation is to ensure that Java is properly installed in your system's PATH.
First, ensure that you have a JDK installed (and not just a JRE). Your next best bet is to add an environment variable called JAVA_HOME and point it at the base install directory for the version of Java that you installed.
Next, modify your PATH environment variable by prepending the string %JAVA_HOME%\bin; to whatever is already in your PATH. If you look at the contents of that directory, you will see that javac is in there (along with a number of other Java development tools).
You have to install JDK (which includes javac compiler) and ensure that it's in the path. See http://java.sun.com/javase/downloads/index.jsp
Common Error Messages from SUN Tutorial page
Step 4 (Update the PATH variable) of the JDK installation instructions explains what you should do to make it so that you can just type javac in the command prompt window.
The link below gives a step by step explanation on how to set up java_home and path variables.
JAVA_HOME setup
Even if I had
JDK installed
JAVA_HOME defined
%JAVA_HOME%\bin present in my %PATH%
I still had this error. I resolved it by eliminating the JRE from my path variable. It was put before JAVA_HOME, has no javac. It seems Windows can't find javac in JAVA_HOME\bin in this case.
For Windows
Control Panel > System...Then click Advanced System Setting (left pane)...then popup window will open...then click Environment Variables...Then another window will open...click New and add enter information below
Variable name: PATH
Variable value: C:\Program Files\Java\jdk1.7.0_45\bin [*make sure this is the right path for you]
Then it should work
Check whether you have JDK installed; if not installed, Install
If JDK is installed, navigate into the bin folder of the installation, and try to execute javac.
If javac executes, proceed (+:
If javac does not execute, check your system path, and java path, and return to 2 above!

Categories

Resources