小程序Jar无法找到资源

时间:2014-02-21 08:18:54

标签: java jar applet

我已经制作了applet,这是来自jar的文件。

enter image description here

在我的课程中,我打电话给ffmpeg.exe,我拥有自签名小程序等所有权限,并通过Access Controller拨打电话。所以我在程序中遇到错误,无法找到*my ffmpeg lib*

这应该很简单问:我应该在哪里放置 ffmpeg.exe文件

我得到例外:

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

1 个答案:

答案 0 :(得分:0)

我从来没有从applet运行.exe,但是我从applet资源加载了一些.dll,首先我将applet资源复制到temp路径,然后从这个路径加载它。 dll位于jar内部,如下所示:

enter image description here

loadLib方法将.dll复制到光盘然后加载它,此方法接收目录和文件名作为参数(在我的情况下,方法调用是loadLib(“/ sun / security / 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;
}

当然为了执行此操作,必须正确签名applet并添加必要的MANIFEST权限。

希望这有帮助,