Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/311.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java SWT应用程序从可运行的.jar或外部文件夹加载.swf文件_Java_Jar_Flash_Swt - Fatal编程技术网

Java SWT应用程序从可运行的.jar或外部文件夹加载.swf文件

Java SWT应用程序从可运行的.jar或外部文件夹加载.swf文件,java,jar,flash,swt,Java,Jar,Flash,Swt,我正在eclipse中使用SWT小部件库进行编程,并正在设计一个可运行的Java应用程序。我已经关闭了应用程序,我只是不知道如何从“所有”计算机上的文件夹加载外部.swf文件。我可以从任何计算机加载图像,因为我使用getResourceAsStream代码行。但是“import com.docuverse.swt.flash.FlashPlayer”“loadMovie(arg,arg)”只接受一个字符串 所以我做了ClassName.class.getResource(“blah.swf”).

我正在eclipse中使用SWT小部件库进行编程,并正在设计一个可运行的Java应用程序。我已经关闭了应用程序,我只是不知道如何从“所有”计算机上的文件夹加载外部.swf文件。我可以从任何计算机加载图像,因为我使用getResourceAsStream代码行。但是“import com.docuverse.swt.flash.FlashPlayer”“loadMovie(arg,arg)”只接受一个字符串

所以我做了ClassName.class.getResource(“blah.swf”).getPath();这给了您一个字符串,我设置了它,并在eclipse上运行它,它可以完美地找到包中的文件。当我导出它时,我制作的可运行Jar在.Jar文件中找不到“blah.swf”

因此,我的问题是,如何从.jar内部或外部文件夹加载.swf文件,以便客户端可以在.jar可执行应用程序旁边下载,以便它可以指向这些swf文件


谢谢。

以下是我在评论中谈到的方式(创建临时文件并将flash动画从jar保存到其中)


我使用了EdnCateDance.swf flash文件,这是SWT flash库中的一个示例。

如果SWT FlashPlayer无法从流中加载电影,则必须从jar中获取的流中创建临时文件,然后由播放器加载。.流输出“file://C://Desktop//application.jar!//blah.swf”如您所见,它尝试在应用程序内部进行搜索。我想出了一个真正肮脏的解决办法。我将“application.jar!”替换为我在桌面上创建的名为“SWF”的文件夹的名称。问题是,如果应用程序和SWF文件夹位于两个不同的目录中,它将无法找到SWF的路径。但是请详细说明你的评论,我对它很困惑。你能发布链接到
com.docuverse.swt.flash.FlashPlayer
jar文件吗?我找不到该项目的任何现有网站。。那我会尽力弄清楚的。当然,给你。请参阅我的答案以了解可能的解决方案,抱歉,这需要一段时间,我正在参加会议;]。。
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

import com.docuverse.swt.flash.FlashPlayer;

public class SWTFlashPlayer {
    private FlashPlayer player = null;

    private final String FLASH_FILE_PATH = "/EdnCateDance.swf";
    private final String TMP_FILE_PREFFIX = "tmp_";
    private final String TMP_FILE_SUFFIX = ".swf";

    private File swfFile = null;

    public SWTFlashPlayer() {
        final Display display = new Display();
        final Shell shell = new Shell(display);
        shell.setLayout(new FillLayout());

        try {
            swfFile = copyFileFromJar(FLASH_FILE_PATH, TMP_FILE_PREFFIX, TMP_FILE_SUFFIX);
        } catch (IOException e) {
            e.printStackTrace();
            return;
        }

        player = new FlashPlayer(shell, SWT.NONE);
        player.loadMovie(0, swfFile.getAbsolutePath());
        player.setSize(150, 150);
        player.activate();

        shell.pack();
        shell.open();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch())
                display.sleep();
        }
        display.dispose();
    }

    /**
     * Copy file packed inside current jar to temp file
     * @param jarPath String - path inside jar
     * @param filePreffix String - temp file preffix
     * @param fileSuffix - temp file suffix
     * @throws IOException - temp file cannot be created or writing somehow fails
     */
    private File copyFileFromJar(String jarPath, String filePreffix, String fileSuffix) throws IOException {
        File toFile = File.createTempFile(filePreffix, fileSuffix);
        // delete file after application finishes
        toFile.deleteOnExit();

        if(!toFile.canWrite()) throw new IOException("File (" + toFile.getPath() + ") not exists or is not writable!");

        FileOutputStream fos = new FileOutputStream(toFile);
        InputStream is = this.getClass().getResourceAsStream(jarPath);

        if(is == null) throw new IOException("File on jar path could not be located or loaded!");

        int read = 0;
        byte bytes[] = new byte[1024];
        while ((read = is.read(bytes)) != -1) {
            fos.write(bytes, 0, read);
        }

        fos.flush();
        fos.close();

        return toFile;
    }

    public static void main(String[] args) {
        new SWTFlashPlayer();
    }
}