简体   繁体   English

从Java中的.java文件构建jar文件

[英]Building a jar file from .java files in java

Hello members of StackOverflow. 您好StackOverflow的成员。

I am wanting to build a jar file in my java application with the following code: 我想使用以下代码在Java应用程序中构建一个jar文件:

    private void javaToClass()
{
    try {
        String path = new File(".").getCanonicalPath();
        String command = "cmd /c javac " + path + "\\files\\*.java";

        System.out.println("Building class files...");
        Runtime.getRuntime().exec(command);
        System.out.println("Class files have been built");
    } catch (IOException e) {
    }
}

private void classToJar()
{
    try {
        String path = new File(".").getCanonicalPath();
        String command = "cmd /c jar cf Built.jar " + path + "\\files\\*.class";

        System.out.println("Building jar file...");
        Runtime.getRuntime().exec(command);
        System.out.println("Jar file has been built");
    } catch (IOException e) {

    }
}

So this builds the jar file but it doesn't add the class files to the jar file. 因此,这将构建jar文件,但不会将类文件添加到jar文件中。 Instead it will add my systems directory: http://i.stack.imgur.com/cTrbO.png 相反,它将添加我的系统目录: http : //i.stack.imgur.com/cTrbO.png

So if anybody could explain to me how i would make it only add my class files it would be a great help. 因此,如果有人可以向我解释我如何使其仅添加我的班级文件,那将是很大的帮助。

Thanks. 谢谢。

Should jar command use -C parameter? jar命令应该使用-C参数吗? Here is my jar syntax, where I give an explicit manifest file as well. 这是我的jar语法,我也给出了一个明确的清单文件。 You can leave that out and remove m option from options list. 您可以将其保留,然后从选项列表中删除m选项。

jar cvfm ./lib/MyLib.jar ./classes/META-INF/MANIFEST.MF -C ./classes . jar cvfm ./lib/MyLib.jar ./classes/META-INF/MANIFEST.MF -C ./classes。

Another tip is you can use unix / path delimiter even in Windows for java commands and java fileio manipulations. 另一个提示是,即使在Windows中,您也可以使用unix /路径分隔符来执行Java命令和Java Fileio操作。 Its a nice way keeping program Unix and Windows compatible. 这是使程序Unix和Windows兼容的好方法。

Doublecheck working directory of shellexec process, you can give an explicit working dir(current dir). 仔细检查shellexec进程的工作目录,可以给出一个明确的工作目录(当前目录)。 Or give an absolute filepaths to all file arguments (jarname, classes folder) 或为所有文件参数(jarname,classes文件夹)提供绝对文件路径
http://docs.oracle.com/javase/7/docs/api/java/lang/Runtime.html#exec%28java.lang.String[],%20java.lang.String[],%20java.io.File%29 http://docs.oracle.com/javase/7/docs/api/java/lang/Runtime.html#exec%28java.lang.String[],%20java.lang.String[],%20java.io。文件%29

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM