无法使用命令行编译Java文件。

huangapple go评论73阅读模式
英文:

Can't compile java file using CLI

问题

我做了一些自动化测试,遇到了一个问题,无法编译java文件。
我的源文件夹是项目的文件夹 "Test"。
在这个文件夹里我有两个jar文件:

<b>hamcrest-core-1.3.jar</b>

<b>junit-4.13.jar</b>

我运行了这个命令:

                javac -cp junit-4.13.jar;. src\r.java 

然后我得到了以下消息:

src\r.java:1: error: package org.junit.jupiter.api does not exist
import static org.junit.jupiter.api.Assertions.fail;
                               ^
src\r.java:1: error: static import only from classes and interfaces
import static org.junit.jupiter.api.Assertions.fail;
^
src\r.java:2: error: package org.junit.jupiter.api does not exist
import static org.junit.jupiter.api.Assumptions.assumeTrue;
                               ^
src\r.java:2: error: static import only from classes and interfaces
import static org.junit.jupiter.api.Assumptions.assumeTrue;
^
src\r.java:4: error: package org.junit.jupiter.api does not exist
import org.junit.jupiter.api.AfterAll;
                      ^
src\r.java:5: error: package org.junit.jupiter.api does not exist
import org.junit.jupiter.api.AfterEach;
                      ^
src\r.java:6: error: package org.junit.jupiter.api does not exist
import org.junit.jupiter.api.BeforeAll;
                      ^
src\r.java:7: error: package org.junit.jupiter.api does not exist
import org.junit.jupiter.api.BeforeEach;
                      ^
src\r.java:8: error: package org.junit.jupiter.api does not exist
import org.junit.jupiter.api.Disabled;
                      ^
src\r.java:9: error: package org.junit.jupiter.api does not exist
import org.junit.jupiter.api.Test;
                      ^
src\r.java:13: error: cannot find symbol
@BeforeAll
^
symbol:   class BeforeAll
location: class r
src\r.java:17: error: cannot find symbol
@BeforeEach
^
symbol:   class BeforeEach
location: class r
src\r.java:21: error: cannot find symbol
@Test
^
symbol:   class Test
location: class r
src\r.java:25: error: cannot find symbol
@Test
^
symbol:   class Test
location: class r
src\r.java:30: error: cannot find symbol
@Test
^
symbol:   class Test
location: class r
src\r.java:31: error: cannot find symbol
@Disabled("for demonstration purposes")
^
symbol:   class Disabled
location: class r
src\r.java:36: error: cannot find symbol
@Test
^
symbol:   class Test
location: class r
src\r.java:42: error: cannot find symbol
@AfterEach
^
symbol:   class AfterEach
location: class r
src\r.java:46: error: cannot find symbol
@AfterAll
^
symbol:   class AfterAll
location: class r
src\r.java:27: error: cannot find symbol
   fail("a failing test");
   ^
symbol:   method fail(String)
location: class r
src\r.java:38: error: cannot find symbol
   assumeTrue("abc".contains("Z"));
   ^
symbol:   method assumeTrue(boolean)
location: class r
src\r.java:39: error: cannot find symbol
   fail("test should have been aborted");
   ^
symbol:   method fail(String)
location: class r
22 errors

我的代码是从 https://junit.org/junit5/docs/current/user-guide/ 上的模板。

import static org.junit.jupiter.api.Assertions.fail;
import static org.junit.jupiter.api.Assumptions.assumeTrue;

import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;

class r {

    @BeforeAll
    static void initAll() {
    }

    @BeforeEach
    void init() {
    }

    @Test
    void succeedingTest() {
    }

    @Test
    void failingTest() {
        fail("a failing test");
    }

    @Test
    @Disabled("for demonstration purposes")
    void skippedTest() {
        // not executed
    }

    @Test
    void abortedTest() {
        assumeTrue("abc".contains("Z"));
        fail("test should have been aborted");
    }

    @AfterEach
    void tearDown() {
    }

    @AfterAll
    static void tearDownAll() {
    }

}

我做错了什么?
请帮忙解决!

英文:

I make some automated tests and faced with an issue that I can't compile the java.file
My source folder is the project's folder "Test".
In this folder i have 2 jar files:

<b>hamcrest-core-1.3.jar</b>

and

<b>junit-4.13.jar</b>

I run this command:

                javac -cp junit-4.13.jar;. src\r.java 

And I got the next message:

src\r.java:1: error: package org.junit.jupiter.api does not exist
import static org.junit.jupiter.api.Assertions.fail;
                               ^
 src\r.java:1: error: static import only from classes and interfaces
 import static org.junit.jupiter.api.Assertions.fail;
 ^
 src\r.java:2: error: package org.junit.jupiter.api does not exist
 import static org.junit.jupiter.api.Assumptions.assumeTrue;
                               ^
 src\r.java:2: error: static import only from classes and interfaces
 import static org.junit.jupiter.api.Assumptions.assumeTrue;
 ^
 src\r.java:4: error: package org.junit.jupiter.api does not exist
 import org.junit.jupiter.api.AfterAll;
                        ^
 src\r.java:5: error: package org.junit.jupiter.api does not exist
 import org.junit.jupiter.api.AfterEach;
                        ^
 src\r.java:6: error: package org.junit.jupiter.api does not exist
 import org.junit.jupiter.api.BeforeAll;
                        ^
 src\r.java:7: error: package org.junit.jupiter.api does not exist
 import org.junit.jupiter.api.BeforeEach;
                        ^
 src\r.java:8: error: package org.junit.jupiter.api does not exist
 import org.junit.jupiter.api.Disabled;
                        ^
 src\r.java:9: error: package org.junit.jupiter.api does not exist
 import org.junit.jupiter.api.Test;
                        ^
 src\r.java:13: error: cannot find symbol
 @BeforeAll
 ^
 symbol:   class BeforeAll
 location: class r
 src\r.java:17: error: cannot find symbol
 @BeforeEach
 ^
 symbol:   class BeforeEach
 location: class r
 src\r.java:21: error: cannot find symbol
 @Test
 ^
 symbol:   class Test
 location: class r
 src\r.java:25: error: cannot find symbol
 @Test
 ^
 symbol:   class Test
 location: class r
 src\r.java:30: error: cannot find symbol
 @Test
 ^
 symbol:   class Test
 location: class r
 src\r.java:31: error: cannot find symbol
 @Disabled(&quot;for demonstration purposes&quot;)
 ^
 symbol:   class Disabled
 location: class r
 src\r.java:36: error: cannot find symbol
 @Test
 ^
 symbol:   class Test
 location: class r
 src\r.java:42: error: cannot find symbol
 @AfterEach
 ^
 symbol:   class AfterEach
 location: class r
 src\r.java:46: error: cannot find symbol
 @AfterAll
 ^
 symbol:   class AfterAll
 location: class r
 src\r.java:27: error: cannot find symbol
    fail(&quot;a failing test&quot;);
    ^
 symbol:   method fail(String)
 location: class r
 src\r.java:38: error: cannot find symbol
    assumeTrue(&quot;abc&quot;.contains(&quot;Z&quot;));
    ^
 symbol:   method assumeTrue(boolean)
 location: class r
 src\r.java:39: error: cannot find symbol
    fail(&quot;test should have been aborted&quot;);
    ^
 symbol:   method fail(String)
 location: class r
 22 errors

My code is the template from https://junit.org/junit5/docs/current/user-guide/

     import static org.junit.jupiter.api.Assertions.fail;
     import static org.junit.jupiter.api.Assumptions.assumeTrue;

     import org.junit.jupiter.api.AfterAll;
     import org.junit.jupiter.api.AfterEach;
     import org.junit.jupiter.api.BeforeAll;
     import org.junit.jupiter.api.BeforeEach;
     import org.junit.jupiter.api.Disabled;
     import org.junit.jupiter.api.Test;

 class r {

@BeforeAll
static void initAll() {
}

@BeforeEach
void init() {
}

@Test
void succeedingTest() {
}

@Test
void failingTest() {
    fail(&quot;a failing test&quot;);
}

@Test
@Disabled(&quot;for demonstration purposes&quot;)
void skippedTest() {
    // not executed
}

@Test
void abortedTest() {
    assumeTrue(&quot;abc&quot;.contains(&quot;Z&quot;));
    fail(&quot;test should have been aborted&quot;);
}

@AfterEach
void tearDown() {
}

@AfterAll
static void tearDownAll() {
}

 }

What I do wrong?
Please, help!

答案1

得分: 1

org.junit.juniper 包表示您的代码使用了 JUnit 5。

但是您的类路径中只有 JUnit 4。

要么按照 JUnit 4 用户指南 进行操作,要么更改您的类路径以包含 JUnit 5。

英文:

The org.junit.juniper packages indicates that your code uses JUnit 5.

But you've only got JUnit 4 on your classpath.

Either follow the JUnit 4 user guide or change your classpath to include JUnit 5.

huangapple
  • 本文由 发表于 2020年8月21日 00:13:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/63509156.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定