3

この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
4

3 に答える 3

4

間違った InputStream を使用しています。これを java.io.InputStream に変更します。

于 2009-09-06T12:55:37.807 に答える
1

org.omg.CORBA.portable.InputStreamjava.io.BufferedInputStream の代わりに、リソースからの入力ストリームをパラメーターとして使用するのはなぜですか。私はこれを意味します:

BufferedInputStream inputstream = new BufferedInputStream(smartpdf.class.getResourceAsStream(...));

ファイル出力ストリームについても同じです: BufferedOutputStream

使用しないでください

class.getResource(...).openStream();

しかし使う

class.getResourceAsStream(...);
于 2009-09-06T12:56:42.960 に答える
0

(問題を解決したら) 生成されたプロセスの stdout と stderr を消費する必要があることに注意してください。そうしないと、生成されたプロセスがブロックされる可能性があります。InputStream詳細については、この回答を参照してください。

于 2009-09-06T13:20:00.617 に答える