java - 在 jar 中调用一个 exe 文件

标签 java jar executable-jar

我正在尝试调用存在此 smartpdf 类的 jar 文件中的“dspdf.exe”。我计划将它提取到一个临时位置并在程序结束时删除。但这似乎不起作用,我们将不胜感激。

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import org.omg.CORBA.portable.InputStream;


public class smartpdf {
 static String url="";
 static String output="output.pdf";

public static void main(String[] args) throws IOException{
 gui mygui = new gui();//gui will call the generate function when user selects
}

 public static void generate() throws IOException{
  InputStream src = (InputStream) smartpdf.class.getResource("dspdf.exe").openStream();
  File exeTempFile = File.createTempFile("dspdf", ".exe");
  FileOutputStream out = new FileOutputStream(exeTempFile);
  byte[] temp = new byte[32768];
  int rc;
  while((rc = src.read(temp)) > 0)
      out.write(temp, 0, rc);
  src.close();
  out.close();
  exeTempFile.deleteOnExit();
  Runtime.getRuntime().exec(exeTempFile.toString()+" "+url+" "+output  );
  //Runtime.getRuntime().exec("dspdf "+url+" "+output);
 }

}

编辑: 我得到的错误:

Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.

Exception in thread "main" java.lang.reflect.InvocationTargetException
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
        at java.lang.reflect.Method.invoke(Unknown Source)
        at org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader.main(JarRsrcLoa
der.java:56)
Caused by: java.lang.ClassCastException: sun.net.www.protocol.jar.JarURLConnecti
on$JarURLInputStream cannot be cast to org.omg.CORBA.portable.InputStream
        at smartpdf.generate(smartpdf.java:18)
        at smartpdf.main(smartpdf.java:14)
        ... 5 more

最佳答案

您使用了错误的 InputStream。将其更改为 java.io.InputStream。

关于java - 在 jar 中调用一个 exe 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1385585/

相关文章:

java - Spring AOP - 从 catch block 中调用建议

java - hadoop java.net.URISyntaxException : Relative path in absolute URI: rsrc:hbase-common-0. 98.1-hadoop2.jar

java - 缺少图像/文件的可运行 JAR(资源)

java - 如何缓存 Spring 应用程序上下文以便在许多 JVM 中使用它

java - 可执行 .Jar 文件中的音乐无法播放

java - ResponseBody 方法和普通方法的多个异常处理程序

java - 在 Java 8 上从源代码编译 Groovy 进行打包是否有令人信服的案例?

java - 在 java web start 中找不到类

java - 如何从可运行 JAR 中读取/写入外部数据资源?

java - 在 netbeans 中构建后,Jar 文件无法正常运行