【问题标题】:Applets Jar cant find resourceApplets Jar 找不到资源
【发布时间】:2014-03-22 15:26:21
【问题描述】:

我已经制作了小程序,这是它的 jar 中的文件。

在我的课程中,我调用了ffmpeg.exe,并且我拥有所有特权,例如自签名小程序并通过访问控制器拨打电话。所以我在程序中遇到错误,找不到*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();
    }
  }
}

【问题讨论】:

  • 代码在做什么(发布它),你得到什么异常(发布完整的堆栈跟踪)?
  • 感谢您的评论。我更新了我的 Q。如您所见,我只是返回 getMessage(在类路径下定位 ffmpeg lib 时收到此异常)。你能建议我如何获取这个小程序的所有 printstacktrace 吗?
  • 我正在执行 cmd 的 cmd 'dir' 并接收 '.../Desktop',所以现在继续前进
  • 是的,我的 *exe 应该放在桌面上,但是为什么?如何改变这个?
  • 您必须从 jar 中提取 exe 文件并将其复制到硬盘驱动器的某个位置(在临时目录中)。

标签: java jar applet


【解决方案1】:

我从不从小程序运行 .exe,但是我从小程序资源加载了一些 .dll,为此首先我将小程序资源复制到临时路径,然后从该路径加载它。 dll位于jar中,如下所示:

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

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

希望这会有所帮助,

【讨论】:

  • 非常感谢您的回答,您能在我的 Q 下回答我最后的评论吗?提前 Tnx。
猜你喜欢
  • 1970-01-01
  • 2014-08-21
  • 2012-02-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-02-09
  • 2012-12-09
  • 2016-06-18
相关资源
最近更新 更多