Java Troubleshooting Java: "javac" Command Not Recognized

  • Thread starter Thread starter JasonRox
  • Start date Start date
  • Tags Tags
    Introduction Java
AI Thread Summary
The discussion centers on common issues faced by beginners in Java programming, specifically regarding the "javac" command not being recognized in the command prompt and a misunderstanding of integer division. To resolve the "javac" issue, users are advised to ensure that the Java SDK's bin directory is included in the PATH environment variable. This can be done through the system properties settings. The conversation also addresses a coding problem where the expression "double y = 2/3;" results in zero due to integer division. The solution involves explicitly casting the integers to doubles or using decimal values, such as "double y = 2.0/3;" or "double y = (double)2/3;". The explanation emphasizes that programming languages process operations in a sequential manner, which is crucial for understanding how to write effective code. This insight helps clarify the importance of data types and casting in Java, ultimately enhancing the learning experience for new programmers.
JasonRox
Homework Helper
Gold Member
Messages
2,381
Reaction score
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
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
 
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.
 
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?
 
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:
Thanks!

I appreciate it.
 
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.
 

Similar threads

Back
Top