0

I need to run a single test case run through the cli. I created a runner class

public class Runner {

    public static void main(String ... args) throws ClassNotFoundException {
        String[] classAndMethod = args[0].split("#");
        Request request = Request.method(Class.forName(classAndMethod[0]),
                classAndMethod[1]);
        Result result = new JUnitCore().run(request);
        System.exit(result.wasSuccessful() ? 0 : 1);
    }
}

using this answer Run single test from a JUnit class using command-line.

I downloaded from github a project (https://github.com/apache/incubator-dubbo) on which I want to run the single method, I positioned myself from terminal on the directory containing the Runner class I created and I launched the following command:

java -cp /path/incubator-dubbo/dubbo-cluster /usr/share/java/junit4-4.12.jar /pathClassRunner/src/com/company/Runner org.apache.dubbo.rpc.cluster.StickyTest#testHeartbeat

but I got the following error:

Error: Could not find or load main class pathClassRunner.src.com.company.Runner

can someone help me?

thanks

broke31
  • 9
  • 6
  • If the first and second parameter are both classpaths, then maybe: `/path/incubator-dubbo/dubbo-cluster;/usr/share/java/junit4-4.12.jar` – Scratte Feb 03 '20 at 17:35
  • I tried to launch the following command: sudo java -cp /pathTo/incubator-dubbo/;/usr/share/java/junit-4.8.2.jar pathToClassRunner/src/com/company/Runner org.apache.dubbo.rpc.cluster.StickyTest#testHeartbeat but I got a: bash: /usr/share/java/junit-4.8.2.jar: Permission denied – broke31 Feb 03 '20 at 17:46
  • I'm no posix expert, but my guess it it's not executable or your user doesn't have access to it. On posix you also have to use `:` not `;` between items in classpath. – Scratte Feb 03 '20 at 17:57

1 Answers1

0

The command expects you to include any paths in the classpath with NO spaces. A space means that the arguments for the classpath has ended. It there is a space in one of your paths you may include double quotes:

java -cp path1:path2:path3 classname argument
java -cp "path1:path2:path3" classname argument

If a path starts with / it's a full path. If not it's relative to where you run your command. The classname must be the name of the class, not the file, so that means no .class. If your class specifies any package, then the classname is required to contain the package name too, and the path is to the root of the package, not the class itself.

Check that your user has proper access to the files, then try:

java -cp /path/incubator-dubbo/dubbo-cluster:/usr/share/java/junit4-4.12.jar:/pathClassRunner/src/com/company Runner org.apache.dubbo.rpc.cluster.StickyTest#testHeartbeat

I'm assuming your Runner is in the default package. In case it's in the com.company package, you'll want to run this instead:

java -cp /path/incubator-dubbo/dubbo-cluster:/usr/share/java/junit4-4.12.jar:/pathClassRunner/src com.company.Runner org.apache.dubbo.rpc.cluster.StickyTest#testHeartbeat

Another assumption is that your Runner.class is in your /pathClassRunner/src... directory.

Scratte
  • 3,056
  • 6
  • 19
  • 26
  • Sorry, but I tried to execute the command like you told me to java -cp Desktop/roba/incubator-dubbo/dubbo-cluster/:/usr/share/java/junit4-4.12.jar: Desktop/roba/script/progetti/customJUnit/src/com/company/Runner org.apache.dubbo.rpc.cluster.StickyTest#testHeartbeat but the result of the terminal is: Error: Could not find or load main class Desktop.roba.script.progetti.customJUnit.src.com.company.Main.java the .class file is in the directory /home/broke31/Desktop/desktop/wardrobe/script/projects/customJUnit/out/production/customJUnit/com – broke31 Feb 03 '20 at 20:42
  • I don't understand why it would say anything ending in `Main.java` when you haven't specified that in your command. – Scratte Feb 03 '20 at 20:57
  • Sorry, I renamed the "Main" class to "Runner", copy and paste error. – broke31 Feb 03 '20 at 21:02
  • You need to specify the class path to your class. Not to your java file. If it's in `/home/broke31/Desktop/desktop/wardrobe/script/projects/customJUnit/out/production/customJUnit/com` then that's what you need to use. Did you put your class in a package? – Scratte Feb 03 '20 at 21:14