简体   繁体   English

Applet Jar无法找到资源

[英]Applets Jar cant find resource

I've made applet, Here's files from its jar. 我做了小程序,这是罐子里的文件。

在此处输入图片说明

In my classes I've call ffmpeg.exe , and I have all privileges like self-signed applet and make call via Access Controller. 在我的课程中,我调用了ffmpeg.exe ,并且拥有所有特权,例如自签名小程序,并可以通过Access Controller进行调用。 So I've getting error in program, It's can't be find *my ffmpeg lib* . 所以我在程序中出现错误,找不到*my ffmpeg lib*

This should be very easy Q: where should I place my ffmpeg.exe file ? 这应该很容易问:我应该在哪里放置ffmpeg.exe文件

And I've get exception as: 我有例外:

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

The code are following as: 代码如下:

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

I never run an .exe from an applet, however I'm load some .dlls from an applet resource, to do this first I copy my applet resources to temp path and then I load it from this path. 我从不从applet运行.exe,但是我从applet资源加载了一些.dll,为此,我首先将applet资源复制到临时路径,然后从该路径加载。 The dlls are located inside the jar how is showed below: dll位于jar内,显示方式如下:

在此处输入图片说明

loadLib method copies .dlls to disc and then load it, this method receive as parameters the directory and the name of the file (in my case the method call is loadLib("/sun/security/mscapi/","sunmscapi_x32.dll"); ) loadLib方法将.dlls复制到光盘上,然后将其加载,该方法将目录和文件名作为参数接收(在我的情况下,方法调用为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;
}

Of course in order to do this operations, the applet must be correct signed and necessary MANIFEST permissions added. 当然,要执行此操作,小程序必须正确签名并添加必要的MANIFEST权限。

Hope this helps, 希望这可以帮助,

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM