简体   繁体   English

通过命令行在Linux中编译Java类

[英]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和TestMain.java。 Main.java compiles and runs smoothly. Main.java编译并运行平稳。 TestMain on the other hand does not. 另一方面,TestMain没有。 TestMain is basically a test class where I use JUnit to handle different scenarios. TestMain本质上是一个测试类,在其中我使用JUnit处理不同的场景。 I instantiate Main in TestMain but the problem is that the compiler cannot find Main.java. 我在TestMain中实例化Main,但是问题是编译器找不到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? 如何使Main类可用于MainTest.java类? Thanks so much! 非常感谢!

In your classpath option, you have set the classpath to only junit-4.10.jar . 在classpath选项中,您已将classpath设置为only junit-4.10.jar You must also include the current directory where your Java files reside. 您还必须包括Java文件所在的当前目录。

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

This includes two paths -- JUnit and the current directory, separated by a : . 这包括两个路径-JUnit和当前目录,以:分隔。 (If this were Windows, then you would use a ; as a separator). (如果是Windows,则应使用;作为分隔符)。

Just another input... 只是另一个输入...

-d can be used to specify the target directory where the compiled class files should be put -d可用于指定应将已编译的类文件放入的目标目录

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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