Search code examples
javajavac

Java Class Files not recognized by ls or ls -a


After I successfully compile both java files using " javac -d /mypath/classes -s /mypath/source filename.java" I run the Main file from the source directory using " java -classpath -d /mypath/classes Main " The terminal will successfully output as follows:

Make: Mazda
Color: White
Year: 1997
Model: Miata MX-5

When I ls either the classes directory or source directory I get nothing. I try ls -l and I total 0 in both directories. I tried ls -a and the terminal tells me " . .. " I think that signifies there are two files in there, but I cant see them at all. How do I make it so I can see the created class files and source files that I compiled with javac?

#Car.java 
public class Car {
    String model;
    String make;
    String color;
    int year;

    public void getCarFacts(){
        System.out.println("Model: " + model);
        System.out.println("Make: " + make);
        System.out.println("Color: " + color);
        System.out.println("Year: " + year);
    }
}

#Main.java
public class Main{
    public static void(String args[]){
        Car myCar
        myCar = new Car();
        myCar.model = "Miata MX-5";
        myCar.make = "Mazda";
        myCar.color = "White";
        myCar.year = 1997;

        myCar.getCarFacts();
    }
}
# ===File Tree===
├── classes
├── source
└── workspace
    ├── Car.java
    └── Main.java

Entire terminal output from start to finish

achrotz@penguin:~/hope/proj1$ tree ./
./
├── classes
├── source
└── workspace
    ├── Car.java
    └── Main.java

3 directories, 2 files
achrotz@penguin:~/hope/proj1$ cd workspace
achrotz@penguin:~/hope/proj1/workspace$ javac -d /hope/proj1/classes -s /hope/proj1/source Car.java
achrotz@penguin:~/hope/proj1/workspace$ javac -d /hope/proj1/classes -s /hope/proj1/source Main.java
achrotz@penguin:~/hope/proj1/workspace$ cd ..
achrotz@penguin:~/hope/proj1$ cd source
achrotz@penguin:~/hope/proj1/source$ java -classpath /hope/proj1/classes Main
Make: Mazda
Color: White
Year: 1997
Model: Miata MX-5
achrotz@penguin:~/hope/proj1/source$ cd ..
achrotz@penguin:~/hope/proj1$ cd source
achrotz@penguin:~/hope/proj1/source$ ls -a
.  ..
achrotz@penguin:~/hope/proj1/source$ ls -l
total 0
achrotz@penguin:~/hope/proj1/source$ ls
achrotz@penguin:~/hope/proj1/source$ cd ..
achrotz@penguin:~/hope/proj1$ cd classes
achrotz@penguin:~/hope/proj1/classes$ ls -a
.  ..
achrotz@penguin:~/hope/proj1/classes$ ls -l
total 0
achrotz@penguin:~/hope/proj1/classes$ ls

Solution

  • You have one directory structure at root level /hope/proj1/... where your source files and classes are then you have the same structure in your home directory that is empty and where you perform your ls, ~/hope/proj1 /...

    This will compile the Car.java file found outside of your home folder

    javac -d /hope/proj1/classes -s /hope/proj1/source Car.java
    

    This will compile the Car.java file found inside of your home folder and store the class file inside the home folder as well

    javac -d ~/hope/proj1/classes -s ~/hope/proj1/source Car.java
    

    but since you seem to have the source files in the workspace folder the above would have to be changed to

    javac -d ~/hope/proj1/classes -s ~/hope/proj1/workspace Car.java