Compiling/Running Java files in Terminal

半城伤御伤魂 提交于 2019-12-09 23:59:30

问题


I'm attempting to run a file that calls multiple files, but I'm getting some errors.

Inside the current directory called day4Measurement, I have 13 files: BuggyMeasurement.java, BuggyMeasurement01.java, BuggyMeasurement02.java, BuggyMeasurement03.java, BuggyMeasurement04.java...BuggyMeasurement10.java, MeasurementTest.java, and Measurement.java.

Measurement.java contains the main() and calls all the other files.

Here's the main():

public static void main(String [] args){
    JUnitCore tester = new JUnitCore();
    String s = "Failed to detect: ";
    int count = 0;
    String [] tests = {"toString prints reverse inches then feet", // 01
        "plus modifies this", // 02
        "minus modifies this", // 03
        "multiple modifies this", // 04
        "plus incorrect roll over", // 05
        "minus incorrect roll over", // 06
        "multiple incorrect roll over", // 07
        "plus incorrect non-roll over", // 08
        "minus incorrect non-roll over", // 09
        "multiple incorrect non-roll over", // 10
        "CORRRRECTTT!!!"
    };
    for (int i = 1; i < tests.length + 1; i++){
        testRound = i;
        System.out.println("Running: " + tests[i-1]);
        TestRunner.run(day4Measurement.MeasurementTest.class);
        Result temp = tester.run(day4Measurement.MeasurementTest.class);
        if (temp.wasSuccessful()) {
            s += tests[i-1] + "; ";
            count++;
        }

    }

    System.out.print(10-(count-1)*0.5 + " ");
    System.out.println(s);
}

In the Mac Terminal, I run

javac Measurement.java

and I get issues. Here's what I get:

Any suggestions?


回答1:


Once you have all the files in a directory (they may be in subdirectories - as long as they are all inside some shared directory), let's call it dir, use the following:

javac -classpath dir Measurement.java

Assuming that you are running the command from the same directory that Measurement.java is in. If not, and either way you are safer this way, make it an explicit path to both dir and Measurement.java, such as:

javac -classpath /home/yourusername/dir /home/yourusername/dir/Measurement.java

This says to the java compiler "I want to compile Measurement.java, here's where it is, and here's where you can find all the class and/or source files that it needs." It will then compile only the files referred to by Measurement.java, so you don't need to worry about accidentally compiling all of your java files ever.




回答2:


One thing to check would be to make sure your directory structure for those files mirrors the package structure. For example, if package for class ABC is com.foo, then your ABC.java file should live in com/foo folder.




回答3:


Compile all the files with javac *.java



来源:https://stackoverflow.com/questions/11236267/compiling-running-java-files-in-terminal

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!