Troubleshooting Java: "javac" Command Not Recognized

In summary: For instance, imagine you wrote this:double x = 3;The compiler would probably try to interpret x as 3.0. This would result in an error, because 3.0 isn't a valid double.
  • #1
JasonRox
Homework Helper
Gold Member
2,386
4
Alright, I did the whole Hello World! thing, but I got stuck on one part.

I downloaded the Sun.Java thing, but now when I go into the command prompt and to try and compile it... the computer does not recognize the "javac" command.

What's going on? Is there another program?
 
Technology news on Phys.org
  • #2
Make sure the javac program is in your PATH environment variable. It's usually in c:\j2sdk1.4.2\bin\, for example.

Right click on My Computer, hit Properties, Advanced, Envrionment Variables, and add this directory to the PATH.

- Warren
 
  • #3
Before you can use the javac command you need to set the path with the path command. Depending on the folder where you jsdk is installed you can type something like this in the command line:
Code:
path "C:\Program Files\j2sdk_nb\j2sdk1.4.2\bin"
and press Enter.
 
  • #4
A simple question about one of the Java exercises they have.

Why is my 2/3 coming out as zero?

Here is the exact line of declaration:

double y = 2/3;

It doesn't get any easier than that. I tried with brackets around it too. I also tried 1/3 + 1/3, with brackets.

The only way it seems to work is like this...

double x=2;
double y=x/3;

This is not efficient at all, nor practical when I go into loops.

What's the problem?
 
  • #5
Because 2 and 3 are integers. You have to explicitly cast these as doubles or use 2.0 and/or 3.0.

Code:
import java.util.*;

public class test {

    public static void main (String args[]) {
        double y = 2.0/3;
        System.out.println(y);
    }
}

[edit] Ok, here's what's going on. Java reads the code on the right of the equal sign and performs whatever task is required. Java then yakes the result and stores that in the desired format of the variable. In your case, you are dividing an integer 2 by the integer three. 3 doesn't go evenly into 2 so the result is zero. 0 is converted to a double(0.0) and then stored into your variable y. The above code is one way around this or you can do the following:

Code:
import java.util.*;

public class test {

    public static void main (String args[]) {
        double y = (double)2/3;
        System.out.println(y);
    }
}

Programming languages are not very smart. They don't see the left and right sides at the same time as we do. The see things as a series of steps. If you start thinking in steps then your code will come a little easier.
 
Last edited:
  • #6
Thanks!

I appreciate it.
 
  • #7
Programming languages are not very smart. They don't see the left and right sides at the same time as we do. The see things as a series of steps. If you start thinking in steps then your code will come a little easier.

So the poster doesn't get the wrong impression, this fact is often a good thing. While it would be nice for the compiler to figure out what we really meant, it would be extraordinarily annoying when the compiler guesses wrong.
 

1. Why is my "javac" command not recognized when I try to compile a Java file?

There could be several reasons for this issue. One possibility is that the JDK (Java Development Kit) is not properly installed on your computer. Make sure you have downloaded and installed the JDK from the official Java website. Another reason could be that the JDK is installed, but the path to the "javac" command is not added to your system's PATH variable. You can check this by typing "javac" in your command prompt or terminal. If it returns an error, you will need to add the path to the JDK bin directory to your PATH variable.

2. I have installed the JDK and added the path to my system's PATH variable, but the "javac" command is still not recognized. What should I do?

If you are using a Windows operating system, make sure you have restarted your computer after adding the JDK path to your PATH variable. This step is necessary for the changes to take effect. If you are using a Mac or Linux, you may need to logout and login again for the changes to take effect.

3. I am certain that the JDK is properly installed and the "javac" command is added to my system's PATH variable, but I am still getting an error. What could be the problem?

It is possible that your JAVA_HOME environment variable is not set correctly. This variable should point to the root directory of your JDK installation. You can check this by typing "echo %JAVA_HOME%" in your command prompt or terminal. If it returns an empty value or an incorrect path, you will need to set the JAVA_HOME variable to the correct path.

4. I have checked all the suggested solutions, but I am still getting the "javac" command not recognized error. What else can I try?

If none of the above solutions work, you may need to reinstall the JDK. Make sure you follow the installation instructions carefully and check for any errors during the installation process. It is also recommended to use the latest version of the JDK to avoid any compatibility issues.

5. Can I use a different compiler instead of "javac" to compile my Java files?

Yes, there are other compilers available for Java, such as Eclipse, NetBeans, and IntelliJ IDEA. However, "javac" is the standard and most commonly used compiler for Java development. It is recommended to resolve the issue with the "javac" command not recognized before exploring other options.

Similar threads

  • Programming and Computer Science
Replies
2
Views
832
  • Programming and Computer Science
Replies
25
Views
3K
  • Programming and Computer Science
Replies
14
Views
2K
  • Programming and Computer Science
Replies
16
Views
2K
  • Programming and Computer Science
Replies
2
Views
5K
  • Programming and Computer Science
Replies
17
Views
5K
  • Programming and Computer Science
Replies
2
Views
2K
  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
Replies
3
Views
15K
  • Programming and Computer Science
Replies
4
Views
2K
Back
Top