类路径上的类文件的ClassNotFoundException

类路径上的类文件的ClassNotFoundException

问题描述:

我有以下代码:

    // Test TODO remove
    try {
        System.out.println(System.getProperties().getProperty("java.class.path"));
        this.getClass().getClassLoader().loadClass("mypackage.MyClass");
    } catch (ClassNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

现在输出显示出该类在类路径上,即:

Now the output shows me, that the class is on the classpath, i.e.:

/...some/path.../workspace/project/target/test-classes:/rest/of/the/classpath
java.lang.ClassNotFoundException: mypackage.MyClass
        ...here be stacktrace...

我还确保了类文件acutaly在给定位置,即该文件存在:

I also made sure, that the class-file acutaly IS in the given location, i.e. this file exists:

/...some/path.../workspace/project/target/test-classes/mypackage/MyClass.class

以下内容可能很重要:所示的代码在附加到jUnit测试的javaagent中执行,我通过编程方式(通过Runtime.execute(...))启动-因此可能存在某些问题除了明显的可能在后台出错的问题外...但仍然:如果类路径包含带有类的文件夹,怎么不能加载呢?

Maybe the following is important: the shown code is executed in a javaagent attached to a jUnit test, that I start programtically (via Runtime.execute(...)) - so there is probably something beyond the obvious that can go wrong in the background... but still: if the classpath contains the folder with the class, how come it cannot be loaded?

对于所有有兴趣的人:
我不知道问题出在哪里。

For all who are interested: I have no idea what the problem was.

我摆弄了一下,结果发现Runtime.exec(...

I fiddled a bit with it, and it turned out that the command string that was executed by Runtime.exec(...) worked well if executed in the shell.

我花了更多时间,但最终放弃了寻找真正原因的搜索。而不是

I fiddled a bit more, but finally gave up searching for the "real" reason. Instead of

Runtime rt = Runtime.getRuntime();
Process proc = rt.exec(command);

我现在使用apache exec:

I now use apache exec:

CommandLine commandLine = CommandLine.parse(command);
DefaultExecutor executor = new DefaultExecutor();
int exitValue = executor.execute(commandLine);

使用完全相同的命令String,突然间它就可以了!

with the exact same command String, and all of a sudden it woks!