Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/unix/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Unix中使用Java运行时类查找Jar文件中的类_Java_Unix - Fatal编程技术网

在Unix中使用Java运行时类查找Jar文件中的类

在Unix中使用Java运行时类查找Jar文件中的类,java,unix,Java,Unix,我正在使用Java运行时类编写一个程序,检查jar文件列表中的类文件。这是我的节目: import java.io.*; public class JavaRunCommand { public static void main(String args[]) { String s = null; try { Process p = Runtime.getRuntime().exec("find /home/user/lib

我正在使用Java运行时类编写一个程序,检查jar文件列表中的类文件。这是我的节目:

import java.io.*;

public class JavaRunCommand {

    public static void main(String args[]) {

        String s = null;

        try {

            Process p = Runtime.getRuntime().exec("find /home/user/lib/ -name \"*.jar\" | xargs grep MyClass.class");

            BufferedReader stdInput = new BufferedReader(new
                 InputStreamReader(p.getInputStream()));

            BufferedReader stdError = new BufferedReader(new
                 InputStreamReader(p.getErrorStream()));

            // read the output from the command
            System.out.println("Here is the standard output of the command:\n");
            while ((s = stdInput.readLine()) != null) {
                System.out.println(s);
            }

            // read any errors from the attempted command
            System.out.println("Here is the standard error of the command (if any):\n");
            while ((s = stdError.readLine()) != null) {
                System.out.println(s);
            }

            System.exit(0);
        }
        catch (IOException e) {
            System.out.println("exception..");
            e.printStackTrace();
            System.exit(-1);
        }
    }
}
当我运行此程序时,会收到以下错误消息:

Here is the standard output of the command:

Here is the standard error of the command (if any):

**find: paths must precede expression: |
Usage: find [-H] [-L] [-P] [-Olevel] [-D help|tree|search|stat|rates|opt|exec] [path...] [expression]**
请告诉我这段代码中缺少了什么,如果我在unix命令行上直接运行命令
find/home/user/lib/-name\“*.jar\”| xargs grep MyClass.class”)
,那么它工作正常

Process p1 = Runtime.getRuntime().exec(new String[] { "/bin/sh", "-c", "cd /home/user/lib/ && find . -name \"*.jar\" | xargs grep MyClass.class" });
另外,如果我用下面的行替换表达式,那么我的Java代码工作正常

Process p1 = Runtime.getRuntime().exec(new String[] { "/bin/sh", "-c", "cd /home/user/lib/ && find . -name \"*.jar\" | xargs grep MyClass.class" });
换成

Process p = Runtime.getRuntime().exec("sh -c find /home/user/lib/ -name \"*.jar");
这样shell就会参与并扩展通配符

但是您实际上不需要在这里运行
find
,您可以自己使用递归调用的
Files.listFiles()
来完成

我也很想看看你接下来在做什么。我会定义一个用JAR列表初始化的
URLClassLoader
,然后用它来尝试加载类,而不是自己解压所有JAR。如果所有JAR都在类路径上:

    Class<?> klazz = Class.forName("sun.security.ec.SunEC", false,
            ClassLoader.getSystemClassLoader());
    CodeSource codeSource = klazz.getProtectionDomain().getCodeSource();
    URL url = codeSource.getLocation();
    System.out.println(url.toExternalForm());

file:/C:/Program%20Files/Java/jre7/lib/ext/sunec.jar
Class klazz=Class.forName(“sun.security.ec.SunEC”,false,
getSystemClassLoader());
CodeSource CodeSource=klazz.getProtectionDomain().getCodeSource();
URL=codeSource.getLocation();
System.out.println(url.toExternalForm());
文件:/C:/Program%20Files/Java/jre7/lib/ext/sunec.jar
要参加的详细信息:
false
表示不进行班级初始化


否则,只要一个文件列表,就可以使用JarFile。在Java 8中,您甚至可以使用并行性。

仅猜测:
*
在用户的shell中得到扩展。感谢Joop,如果我使用我的帖子中提到的第二个表达式(它也有
*
)然后程序运行正常。所以我的第一个表达式中似乎缺少了一些东西。实际上……为什么不使用find+-exec,其中执行的程序是jar tf{}| grep?您不需要Java程序…但在这种查找中,通配符“*”不能由shell扩展:find使用它来匹配目标目录下的所有文件!”感兴趣的…接下来“我只需添加一个-exec来使用“jar tf{}”进行查找,并将其导入到所需类的grep中。如果它都在那里,为什么要使用程序;-)