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.
1 Answer
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...
3 Comments
winkyloCC
The ls command was what I needed to at least show that both files were being loaded. I need to go over a list of commands for terminal. Upon compiling the file, I get a note that the file uses unchecked or unsafe operation, which I don't understand since Netbeans doesn't have a problem with it. Asks to recompile with -Xlint. Went a step further and tried calling the java VM but returns a could not find or load main class Driver.
CXJ
Did you try compiling with Xlint to see what it says? It should look like "$ javac -Xlint *.java".
winkyloCC
Here is what Xlint came back with. I'm looking at it now to make heads or tales of it. TotalComp.java:49: warning: [rawtypes] found raw type: ArrayList ArrayList al1 = new ArrayList(); ^ missing type arguments for generic class ArrayList<E> where E is a type-variable: E extends Object declared in class ArrayList
javac -classpath "filename.java"does not "run" Java files. It invokes the compiler.