winkyloCC
winkyloCC

Reputation: 31

Using terminal to run java files

I'm working on a Mac, and use xcode to save my java files. I have a file that runs without error in netbeans. The file has another file in the same folder that acts as the subclass. When I run the file in terminal I suspect that the subclass file is not running because of an multiple can not find symbol errors that come up. Any ideas why? I call the file by using the cd command until I arrive at the folder with the files. I then use the (javac -classpath "filename.java")to run the file.

Upvotes: 0

Views: 4115

Answers (1)

CXJ
CXJ

Reputation: 4449

A simple example from my Mac, which might be of some help to you.

List of the files in my directory:

$ ls *.java
Child.java   Driver.java  Parent.java

Display the contents of all three files:

$ cat *.java
// file Child.java
public class Child extends Parent {
    public Child() {
        System.out.println("  I'm the Child...");
    }
}

// file Driver.java
public class Driver {
    public static void main(String[] args) {
        Parent parent = new Parent();
        parent.hello();
        Child child = new Child();
    }
}

// file Parent.java
public class Parent {
    public Parent() {
    }
    public void hello() {
        System.out.println("Hello from the parent.");
    }
}

Compile all 3 Java source files into byte code:

$ javac *.java

Call the Java VM to execute the main entry point:

$ java Driver
Hello from the parent.
  I'm the Child...

Upvotes: 2

Related Questions