简体   繁体   中英

Compiling Java classes in linux via command line

Hi and thanks for taking the time to answer my questions.

I have two files in my root folder (~/). The Main.Java and TestMain.java. Main.java compiles and runs smoothly. TestMain on the other hand does not. TestMain is basically a test class where I use JUnit to handle different scenarios. I instantiate Main in TestMain but the problem is that the compiler cannot find Main.java.

Here's the code:

    user@linuxbox ~ $ javac -cp junit-4.10.jar TestMain.java 
    TestMain.java:8: error: cannot find symbol
                Main mainClass = new Main();
                ^
      symbol:   class Main
      location: class TestMain
    TestMain.java:8: error: cannot find symbol
                Main luckyStrings = new Main();
                                                ^
      symbol:   class Main
      location: class TestMain
    2 errors

How can I make the Main class available to the MainTest.java class? Thanks so much!

In your classpath option, you have set the classpath to only junit-4.10.jar . You must also include the current directory where your Java files reside.

javac -cp "junit-4.10.jar:." TestMain.java

This includes two paths -- JUnit and the current directory, separated by a : . (If this were Windows, then you would use a ; as a separator).

Just another input...

-d can be used to specify the target directory where the compiled class files should be put

javac -d . -cp "junit-4.10.jar:." TestMain.java

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM