简体   繁体   English

java:将文件添加到jar目录

[英]java: add a file to a jar directory

I am working on a project which requires users to upload a Java file which can be later compiled into a class file. 我正在研究一个项目,该项目要求用户上载Java文件,该文件以后可以编译成类文件。 I have already written the code for uploading the file to the project folder. 我已经编写了将文件上传到项目文件夹的代码。 It works perfectly when launched using Eclipse. 使用Eclipse启动时,它可以完美运行。 The problem arises when I package the project as a JAR. 当我将项目打包为JAR时,就会出现问题。 The code throws an exception. 该代码引发异常。 I understand the reason.. when we package the project, only the class files are loaded in JAR and not the source files, and the location of the JAR file also makes a difference. 我了解原因。.当我们打包项目时,仅将类文件加载到JAR中,而不是源文件中,并且JAR文件的位置也有所不同。

Let me explain what I have done and what I need to do: 让我解释一下我已经做过的事情以及我需要做的事情:

In the IDE version of the project, I have something like: 在该项目的IDE版本中,我有类似以下内容: 在此处输入图片说明
where the user can browse for a file and click on the upload button to add it to the project structure. 用户可以在其中浏览文件,然后单击上载按钮将其添加到项目结构中。 This works perfectly with the following code: 这与以下代码完美配合:

String path = Paths.get(".").toAbsolutePath().normalize().toString()+"\\path\\to\\directory";
File source=new File(textField1.getText());
String fileName="";
try {
    fileName=new java.io.File(textField1.getText()).getName();
    Files.copy(source.toPath(),new java.io.File(path+fileName).toPath(),REPLACE_EXISTING);
} catch (IOException e2) {
    e2.printStackTrace();
}

When I have to run the same code using a JAR file, I understand that I am supposed to change how I fetch the root directory of the project and then navigate to the required directory. 当我必须使用JAR文件运行相同的代码时,我知道应该更改获取项目根目录的方式,然后导航到所需目录。 Somehow, I can't get it working. 不知何故,我无法正常工作。 I have already tried the following: 我已经尝试了以下方法:

URL location=MainClass.class.getProtectionDomain().getCodeSource().getLocation();
String p = URLDecoder.decode(location.getFile(), "UTF-8");
System.out.println(location);

This prints rsrc:./ . 这将输出rsrc:./ How can I copy the uploaded file to the required directory within the Jar file? 如何将上传的文件复制到Jar文件中的所需目录?

I also tried another approach where I thought of creating a shell script to execute jar uf jar-file input-file(s) . 我还尝试了另一种方法,我想到创建一个shell脚本来执行jar uf jar-file input-file(s) I put the JAR file and this shell script in the same directory, and execute the following code: 我将JAR文件和此Shell脚本放在同一目录中,并执行以下代码:

try {
        String fileName = "shellFile.sh";
        URL location=MainClass.class.getProtectionDomain().getCodeSource().getLocation();
        String p = URLDecoder.decode(location.getFile(), "UTF-8");
        System.out.println(location);
        String inputFilePath = p + fileName;
        System.out.println(inputFilePath);
        Process process = Runtime.getRuntime().exec(inputFilePath);
    } catch (IOException e2) {
        // TODO Auto-generated catch block
        e2.printStackTrace();
    }

but I get the following exception: 但我得到以下异常:

java.io.IOException: Cannot run program "./shellFile.sh": CreateProcess error=2, The system cannot find the file specified at java.lang.ProcessBuilder.start(Unknown Source) java.io.IOException:无法运行程序“ ./shellFile.sh”:CreateProcess错误= 2,系统找不到在java.lang.ProcessBuilder.start中指定的文件(未知源)

Is there any way I can upload a file to a directory within the JAR file? 有什么方法可以将文件上传到JAR文件中的目录?

String cmd = "jar uvf " + <jar file name> + " " + <name of file to be added;
System.out.println(cmd);
Process p = Runtime.getRuntime().exec(cmd);

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

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