小程序的Jar找不到资源 [英] Applets Jar cant find resource

查看:246
本文介绍了小程序的Jar找不到资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做的小程序,在这里,从它的JAR的文件。

在我的班级我已经调用 ffmpeg.exe ,和我有一个自签名的小应用程序的所有权限,并通过门禁控制器进行呼叫。所以,我在节目得到错误,这是不能被发现 *我的ffmpeg的lib *

这应该是很容易问:我应该在哪里把我的 ffmpeg.exe文件

和我已经得到的异常为:

 不能运行程序的ffmpeg:CreateProcess的错误= 2,?? ??? ??? ????? ????

在code的以下为:

 公共类DtpVideoApplet扩展的Applet
{
  公共字符串的startRecording()抛出IOException异常
  {
  尝试
  {
    返回(字符串)在AccessController.doPrivileged(新的PrivilegedAction<串GT;()
    {
    公共字符串的run()
    {
    尝试
    {
    调用Runtime.getRuntime()
    .exec(的ffmpeg -y -f DSHOW -i视频= \\屏幕捕获记录仪\\output.flv);
    返回进​​入;
    }
    赶上(例外五)
    {
    // TODO自动生成catch块
    返回e.getMessage();
    }
    }
    });
    }
    赶上(例外五)
    {
    返回e.getMessage();
    }
  }
}


解决方案

我永远不会运行从applet的.exe,但我是负载从applet一些资源.dll文件,首先我在我的小应用程序的资源复制到做到这一点临时路径,然后我从此路径加载它。该DLL是位于罐内如何显示如下:

LOADLIB方法复制到.DLL文件光盘,然后加载它,这种方法得到的参数的目录和文件名(在我的情况的方法调用LOADLIB(/阳光/安全/ MSCAPI /,sunmscapi_x32 .DLL);)

 公共静态无效LOADLIB(字符串libraryPath,字符串库名称)抛出IOException异常,InterruptedException的{    的System.out.println(libraryPath +---------------------);
    URL inputStreamLibURL = AddSunMSCAPIProvider.class.getResource(libraryPath);
    如果(inputStreamLibURL == NULL){
        抛出新IOException异常(未找到资源:+ libraryPath);
    }    字符串TEMPPATH = System.getProperty(java.io.tmpdirNOT_FOUND);
    如果(tempPath.equals(NOT_FOUND)){
        抛出新IOException异常(临时找不到文件);
    }
    文件的tempDir =新的文件(TEMPPATH);    //首先尝试覆盖默认文件
    文件defaultFile =新的文件(TEMPPATH,库名称);    布尔useDefaultFile = FALSE;
    如果(defaultFile.exists()){
        尝试{
            useDefaultFile = defaultFile.delete();
            //返回FALSE如果库不能被删除(锁定)
        }赶上(例外五){
            e.printStackTrace();
            useDefaultFile = FALSE;
        }
    }其他{
        useDefaultFile = TRUE;
    }    文件临时文件;
    如果(useDefaultFile){
        TEMPFILE = defaultFile;
    }其他{
        TEMPFILE = File.createTempFile(库名称,,的tempDir);
    }    副本(inputStreamLibURL.openStream(),临时文件,0);    。调用Runtime.getRuntime()负荷(tempFile.getAbsolutePath());
}/ **
 *文件复制
 * @参数SRC
 * @参数DEST
 * @参数BUFFERSIZE
 *引发IOException
 * /
私有静态无效副本(SRC的InputStream,文件DEST,INT缓冲区大小)抛出IOException
    如果(BUFFERSIZE&下; = 0){
        BUFFERSIZE = 2000; //默认的ByteBuffer
    }
    InputStream为= SRC;
    OutputStream的OS =新的BufferedOutputStream(新的FileOutputStream(DEST));
    字节[]缓冲区=新的字节[缓冲区大小]
    INT℃;
    而((C = is.​​read(缓冲液))!= - 1){
        os.write(缓冲,0,C);
    }
    is.close();
    os.close();
    返回;
}

课程,为了做到这一点行动中,小应用程序必须是正确的签署和必要的清单权限补充说。

希望这有助于

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. So I've getting error in program, It's can't be find *my ffmpeg lib*.

This should be very easy Q: where should I place my ffmpeg.exe file ?

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. The dlls are located inside the jar how is showed below:

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

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.

Hope this helps,

这篇关于小程序的Jar找不到资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆