有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java jar编译运行时。getRuntime。exec()jar中的完整目录

只是为了好玩,我在dropbox上制作了一个小Java项目文件,以便在没有ide的情况下为我编译Java,这比我自己键入那些讨厌的命令行参数要容易得多

现在我只有一个小问题。。。 首先,这是我的代码,它确实能够将类文件编译成带有清单的jar

    public static String listFilesString(String dirLocation){
    String allPaths = ""; //pretty self explanatory returns full list of files in directory with spaces
    File f = new File(dirLocation);
    if(f.isDirectory()&&f.list().length>0){
        for(File f2 : f.listFiles()){
            if(f2.isDirectory()){
                allPaths = allPaths + listFilesString(f2.toString());
            } else {
                allPaths = allPaths + f2.toString() + " ";
            }
        }
    }
    return allPaths;
}

public static boolean compileOutputToJar(String output, String jarLocation){
    output = output.replace('\\', '/'); //replacements just for uniformity
    String binF = WorkspaceVariables.workspaceDir+output;
    String toCompile = listFilesString(binF).replace('\\', '/');
    try {
        Runtime.getRuntime().exec("jar cvfm " + jarLocation + " " + binF + "manifest.txt " + toCompile); // this line represents the problem
        System.out.println("Compiled Workspace to Jar!");
        return true;
    } catch (IOException e) {
        e.printStackTrace();
        return false;
    }
}

正如在包含运行时的行上所评论的那样。getRuntime()。exec(“jar cvfm”+jarLocation+“”+binF+“manifest.txt”+toCompile);这就是问题所在。的确,该命令可以正确执行,但我确实提供了要编译到jar中的类文件的完整路径

作为一个例子,我将使用这个编译的示例项目。目录结构为:

/bin/manifest.txt     <   The manifest is compiled properly
/bin/Main.class       <   Calls k.Me to get the n variable which is printed
/bin/k/Me.class       <   Defines a string 'n' equal to "hi"

然而,它被编译到jar中,如下所示:

META_INF/MANIFEST.MF
Users/MYUSERNAME/Desktop/Other/ide/javas/bin/Main.class
Users/MYUSERNAME/Desktop/Other/ide/javas/bin/manifest.txt < Nevermind this inclusion, just a problem I've not fixed.
Users/MYUSERNAME/Desktop/Other/ide/javas/bin/k/Me.class

问题很明显,文件在这样运行时无法运行,而且显然编译错误。我可以通过在执行之前更改到它们所在的目录来正确编译它(没有找到这样做的方法)。或者可能在命令执行期间更改位置(我尝试过使用-cp,但没有效果)

最好的选择似乎是使用-C,因为它可以移动主管道。类别和舱单。txt到正确的位置,但它不包括子目录给我。类和k文件夹不再存在。在每个文件名的开头加上“-C”+f2。getParent()+“”。在中,ListFileString方法阻止任何类文件编译到jar中

感谢您的帮助/贡献


共 (0) 个答案