2014-02-21 54 views
0

我已經做了小程序,這是從它的jar文件。Applets罐不能找到資源

enter image description here

在我的班級我已經打電話ffmpeg.exe,和我有這樣的自簽名的小程序的所有權限,並通過門禁控制器進行呼叫。所以我在程序中出錯,找不到*my ffmpeg lib*

這應該很容易問:我應該在哪裏放置我的ffmpeg.exe文件

而且我已經GET例外,因爲:

Cannot run program "ffmpeg": CreateProcess error=2, ?? ??? ??? ????? ???? 

的代碼以下爲:

public class DtpVideoApplet extends Applet 
{ 
    public String startRecording() throws IOException 
    { 
    try 
    { 
    return(String) AccessController.doPrivileged(new PrivilegedAction<String>() 
    { 
    public String run() 
    { 
    try 
    { 
    Runtime.getRuntime() 
    .exec("ffmpeg -y -f dshow -i video=\"screen-capture-recorder\" output.flv"); 
    return "Entered"; 
    } 
    catch (Exception e) 
    { 
    // TODO Auto-generated catch block 
    return e.getMessage(); 
    } 
    } 
    }); 
    } 
    catch (Exception e) 
    { 
    return e.getMessage(); 
    } 
    } 
} 
+0

什麼是代碼做(張貼),和你得到了什麼異常(發佈完整的堆棧跟蹤)? –

+0

感謝您的評論。我更新了我的Q.正如你所看到的,我只是返回getMessage(我在收到這個異常的同時,在classpath下找到ffmpeg lib wasnt)。你能告訴我如何獲得這個applet的所有printstacktrace嗎? – user2171669

+0

我正在執行cmd的cmd'dir'並收到'.../Desktop',所以現在就向前移動 – user2171669

回答

0

我從來沒有從一個小程序運行.exe,但是我加載一些.DLL文件從一個小程序資源,首先我做我的小程序資源複製到臨時路徑,然後我從這個路徑加載它。這些dll位於罐內如何表現如下:

enter image description here

LOADLIB方法複製到.DLL文件光盤,然後加載它,這種方法得到的參數的目錄和文件名(在我的情況該方法調用LOADLIB(「/陽光/安全/ MSCAPI /」,「sunmscapi_x32.dll」);)

public static void loadLib(String libraryPath, String libraryName) throws IOException, InterruptedException{ 

    System.out.println(libraryPath + "---------------------"); 
    URL inputStreamLibURL = AddSunMSCAPIProvider.class.getResource(libraryPath); 
    if(inputStreamLibURL==null){ 
     throw new IOException("Resource not found: " + libraryPath); 
    } 

    String tempPath = System.getProperty("java.io.tmpdir", NOT_FOUND); 
    if(tempPath.equals(NOT_FOUND)){ 
     throw new IOException("Temporary File not found"); 
    } 
    File tempDir = new File(tempPath); 

    //first try to overwrite the default file 
    File defaultFile = new File(tempPath, libraryName); 

    boolean useDefaultFile = false; 
    if(defaultFile.exists()){ 
     try{ 
      useDefaultFile = defaultFile.delete(); 
      //return false if the library cannot be deleted (locked) 
     }catch(Exception e){ 
      e.printStackTrace(); 
      useDefaultFile = false; 
     } 
    }else{ 
     useDefaultFile = true; 
    } 

    File tempFile; 
    if(useDefaultFile){ 
     tempFile = defaultFile; 
    }else{ 
     tempFile = File.createTempFile(libraryName, "", tempDir); 
    } 

    copy(inputStreamLibURL.openStream() ,tempFile, 0); 

    Runtime.getRuntime().load(tempFile.getAbsolutePath()); 
} 

/** 
* File copy 
* @param src 
* @param dest 
* @param bufferSize 
* @throws IOException 
*/ 
private static void copy(InputStream src, File dest, int bufferSize) throws IOException{ 
    if(bufferSize<=0){ 
     bufferSize = 2000; //default bytebuffer 
    } 
    InputStream is = src; 
    OutputStream os = new BufferedOutputStream(new FileOutputStream(dest)); 
    byte[] buffer = new byte[bufferSize]; 
    int c; 
    while((c = is.read(buffer))!= -1){ 
     os.write(buffer, 0, c); 
    } 
    is.close(); 
    os.close(); 
    return; 
} 

當然,爲了做到這一點的操作,小程序必須正確簽署和必要的清單權限添加。

希望這有助於

+0

Tnx你很多的答案,你能回答我的最後在我的Q下發表評論? Tnx提前。 – user2171669