【问题标题】:jar compile Runtime.getRuntime.exec() full directory in jarjar 编译 Runtime.getRuntime.exec() jar 中的完整目录
【发布时间】:2016-06-08 23:56:24
【问题描述】:

只是为了好玩,我正在制作一个小的 Java 项目文件以保存在我的 Dropbox 上,以便为我编译 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"

然而,这被编译到 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 因为它可以将 Main.class 和 manifest.txt 移动到适当的位置,但是它不包括 Me.class 的子目录并且 k 文件夹不再存在。并通过添加“-C”+ f2.getParent()+“”将其添加到每个文件名的开头。 listFilesString 方法中的所有类文件都无法编译到 jar 中。

感谢任何帮助/贡献!

【问题讨论】:

  • 您是否考虑过 (a) makefile (2) Maven (3) IDE?这是一个已解决的问题。
  • 整个想法是我可以制作一个快速项目并在没有外部实用程序的情况下对其进行编译。
  • 你最终会重新实现 make 或 Maven 或 IDE。走这条路也没有意义。
  • 这适用于例如我在学校时,我无法运行其他编译器,我可以在简单的文本编辑器中编写小程序并从 JDK / JRE 上编译和运行它一个USB。我已经开始了,我不打算改变路线。它可以比使用您建议的方法更有效、更快速地完成。
  • 原来给定的方法完美编译了jar,我忘记在要编译的文件中包含包名。但是能够在没有 make、Maven 或 IDE 的情况下实现编译

标签: java jar compilation runtime.exec


【解决方案1】:

缺少一行,稍作调整已更正错误。

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;
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-09-23
    • 1970-01-01
    • 2022-11-01
    • 2012-01-11
    • 2020-03-02
    • 2014-06-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多