Question

I am trying to call the "dspdf.exe" inside the jar file where this smartpdf class exists. I plan to extract it to a temp location and delete when program ends. However this doesn't seem to work, any help will be appreciated.

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);
 }

}

EDIT: The error that I am getting:

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
Was it helpful?

Solution

You use the wrong InputStream. Change it to java.io.InputStream.

OTHER TIPS

Why do you use org.omg.CORBA.portable.InputStream instead of a java.io.BufferedInputStream` with as parameters the inputstream from the resource. I mean this:

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

The same for your fileoutput stream: BufferedOutputStream

Don't use

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

but use

class.getResourceAsStream(...);

Note also (once you've resolved the InputStream issue) that you should be consuming your spawned process stdout and stderr, otherwise the spawned process may block. See this answer for more details.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top