views:

649

answers:

4

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
+1  A: 

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

ZZ Coder
A: 

Hello,

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(...);
Martijn Courteaux
A: 
package pk;

import java.io.*;

public class Test{
  public Test() throws Exception {
        // Place .exe into the package folder. 
       InputStream src =this.getClass().getResourceAsStream("sample.exe");
    if(src!=null) {
       File exeTempFile = File.createTempFile("dspdf", ".exe");
       byte []ba=new byte[src.available()];
       src.read(ba,0,ba.length);
       exeTempFile.deleteOnExit();  
       FileOutputStream os=new FileOutputStream(exeTempFile);
       os.write(ba,0,ba.length);
       os.close();

       String []args=new String[3];
       args[0]=exeTempFile.getAbsolutePath();
       args[1]="One";
       args[2]="Two";
        Process ps=Runtime.getRuntime().exec(args);
        ps.waitFor();
        java.io.InputStream is=ps.getInputStream();
        byte b[]=new byte[is.available()];
        is.read(b,0,b.length);
        System.out.println(new String(b));
      }
    else
       System.out.println("Executable not found");
  } 
  public static void main(String []args)  throws Exception{
    new Test();
  }
}
adatapost
A: 

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.

Brian Agnew