Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/318.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
Java jar编译运行时.getRuntime.exec()jar中的完整目录_Java_Jar_Compilation_Runtime.exec - Fatal编程技术网

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

Java jar编译运行时.getRuntime.exec()jar中的完整目录,java,jar,compilation,runtime.exec,Java,Jar,Compilation,Runtime.exec,只是为了好玩,我在dropbox上制作了一个小Java项目文件,以便在没有ide的情况下为我编译Java,这比我自己键入那些讨厌的命令行参数要容易得多 现在我只有一个小问题。。。 首先,这是我的代码,它确实能够将类文件编译到带有清单的jar中 public static String listFilesString(String dirLocation){ String allPaths = ""; //pretty self explanatory returns full l

只是为了好玩,我在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;
    }
}
正如在包含Runtime.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"
/bin/manifest.txt<清单编译正确
/bin/Main.class<调用k.Me以获取打印的n变量
/bin/k/Me.class<定义一个等于“hi”的字符串“n”
但是,它被编译到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
META\u INF/MANIFEST.MF
Users/MYUSERNAME/Desktop/Other/ide/javas/bin/Main.class
Users/MYUSERNAME/Desktop/Other/ide/javas/bin/manifest.txt<不必考虑这个问题,我还没有解决这个问题。
Users/MYUSERNAME/Desktop/Other/ide/javas/bin/k/Me.class
问题很明显,文件无法在这样的情况下运行,而且显然编译错误。我可以通过在执行之前更改到它们所在的目录来正确编译它(没有找到这样做的方法)。或者可能在命令执行期间更改位置(我尝试使用-cp,但没有效果)

最好的选择似乎是使用-C,因为它可以将Main.class和manifest.txt移动到适当的位置,但是它不包括Me.class的子目录,并且k文件夹不再存在。并通过添加“-C”+f2.getParent()+”,将其添加到每个文件名的开头。在中,ListFileString方法完全阻止任何类文件编译到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 + f2.toString() + " "; //THIS LINE WAS ADDED
                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;
    }
}

您是否考虑过(a)makefile(2)Maven(3)IDE?这是一个已解决的问题。整个想法是我可以快速创建一个项目并在没有外部实用程序的情况下编译它。最终您将重新实现
make
或Maven或IDE。如果我在学校不能运行其他编译器,我可以在一个简单的文本编辑器中编写一些小程序,然后在USB上通过JDK/JRE进行编译和运行。我已经开始了,我不打算改变方向。它可以比使用您建议的方法更有效、更快地完成。最初给定的方法可以完美地编译jar,我忘记了在要编译的文件中包含包名。但是能够在没有make、Maven或IDE的情况下实现编译