繁体   English   中英

执行存在于jar文件中的批处理文件

[英]Executing a batch file that lives inside a jar file

我有一个Java项目,该项目将用作可执行文件。 如果我的项目在其/ resources文件夹中包含一个批处理文件,该如何对我的项目进行编码,以便当可执行文件.jar运行时可以在内部调用该批处理文件?

编辑:这是Windows 7批处理文件

假设您的批处理文件位于“ com.mycompany.MyBatch.bat”的类路径中

您首先需要将文件复制到tmp ,然后使其可执行并运行:

public static void main(String[] args) throws Exception {
    final File tmp = File.createTempFile("myBatch", "batch");
    try (final ReadableByteChannel channel = Channels.newChannel(App.class.getResourceAsStream("/com/mycompany/MyBatch.bat"));
            final FileChannel fileChannel = new RandomAccessFile(tmp, "rw").getChannel()) {
        final ByteBuffer bb = ByteBuffer.allocate(1024);
        while (channel.read(bb) > 0) {
            bb.flip();
            fileChannel.write(bb);
            bb.clear();
        }
    }
    tmp.setExecutable(true);
    //run script
}

暂无
暂无

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

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