分发包含内容的Java程序

分发包含内容的Java程序,java,netbeans,Java,Netbeans,在过去的几个月里,我一直在为一个大学项目用java编写一个游戏。这是即将结束的项目,我想把项目编译成一个单一的文件,很容易分发。游戏当前从IDE内部运行,并依赖于在特定位置设置的工作目录(即带有声音/纹理等的内容目录)。为了便于携带,最好的方法是什么?我希望有办法将内容编译成jar文件 注意。我使用的是NetBeans,因此任何在NetBeans中简单的解决方案都会获得额外的积分;) 增编: 作为将来的参考,我找到了一种按目录访问东西的方法,这可能不是最好的方法,但它很有效: File dire

在过去的几个月里,我一直在为一个大学项目用java编写一个游戏。这是即将结束的项目,我想把项目编译成一个单一的文件,很容易分发。游戏当前从IDE内部运行,并依赖于在特定位置设置的工作目录(即带有声音/纹理等的内容目录)。为了便于携带,最好的方法是什么?我希望有办法将内容编译成jar文件

注意。我使用的是NetBeans,因此任何在NetBeans中简单的解决方案都会获得额外的积分;)

增编:

作为将来的参考,我找到了一种按目录访问东西的方法,这可能不是最好的方法,但它很有效:

File directory = new File(ClassLoader.getSystemResource("fullprototypeone/Content/Levels/").toURI());

现在我可以像平常一样使用这个文件对象了

这是我的使用手册。这些技术只针对您描述的任务而存在。

一种方法是通过Class.getResourceAsStream(或Class.getResource)访问资源。然后确保这些文件在JAR文件中

在我看来(不用尝试),您应该能够将资源与源文件一起放入,从而使NetBeans将它们放入JAR文件中。然后将文件内容更改为getResource调用

我建议制作一个播放声音的简单程序,并在您尝试转换整个项目之前进行尝试


如果您尝试了,但它不起作用,请让我知道,我会看看是否可以再深入一点(如果是这样的话,发布简单项目的代码会很好)。

您可以在jar文件中嵌入资源-这毕竟只是一个zip文件。实现这一点的标准方法是将资源文件放在源层次结构中的某个目录中。然后通过Object.getClass().getResourceAsStream()引用它们。因此,您需要更改在代码中检索它们的方式

您可以在此处阅读更多:。当然,您使用的不是对象而是包中的某个类


当您将这些资源文件放在src层次结构中时,我相信Netbeans应该使用项目的标准构建为您提供jar!将编译的.class文件和资源以及文件夹放在jar文件中。。您还必须构建一个清单文件。。您可以找到关于在上创建.jar文件的教程
谷歌。您很可能会访问java.sun.com

这是我在另一条评论中承诺的代码。。。这不是我所记得的,但它可能会让你开始

实际上,您可以调用:stringfilename=FileUtils.getFileName(Main.class,“foo.txt”)

它会在磁盘或JAR文件中找到该文件。如果它在JAR文件中,它会将它提取到一个临时目录。然后,您可以使用“新文件(文件名)”打开该文件,该文件无论以前在何处,都将位于磁盘上

我要做的是看看getFile方法,看看如何使用JAR文件来迭代它的内容,并在给定的目录中查找文件

就像我说的,这不完全是你想要的,但确实为你做了很多前期工作

import java.io.Closeable;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLDecoder;
import java.security.CodeSource;
import java.security.ProtectionDomain;
import java.util.zip.ZipEntry;
import java.util.zip.ZipException;
import java.util.zip.ZipFile;


public class FileUtils
{
    public static String getFileName(final Class<?>  owner,
                                     final String    name)
        throws URISyntaxException,
               ZipException,
               IOException
    {
        String    fileName;
        final URI uri;

        try
        {
            final String external;
            final String decoded;
            final int    pos;

            uri      = getResourceAsURI(owner.getPackage().getName().replaceAll("\\.", "/") + "/" + name, owner);
            external = uri.toURL().toExternalForm();
            decoded  = external; // URLDecoder.decode(external, "UTF-8");
            pos      = decoded.indexOf(":/");
            fileName = decoded.substring(pos + 1);
        }
        catch(final FileNotFoundException ex)
        {
            fileName = null;
        }

        if(fileName == null || !(new File(fileName).exists()))
        {
            fileName = getFileNameX(owner, name);
        }

        return (fileName);
    }

    private static String getFileNameX(final Class<?> clazz, final String name)
        throws UnsupportedEncodingException
    {
        final URL    url;
        final String fileName;

        url = clazz.getResource(name);

        if(url == null)
        {
            fileName = name;
        }
        else
        {
            final String decoded;
            final int    pos;

            decoded  = URLDecoder.decode(url.toExternalForm(), "UTF-8");
            pos      = decoded.indexOf(":/");
            fileName = decoded.substring(pos + 1);
        }

        return (fileName);
    }

    private static URI getResourceAsURI(final String    resourceName,
                                       final Class<?> clazz)
        throws URISyntaxException,
               ZipException,
               IOException
    {
        final URI uri;
        final URI resourceURI;

        uri         = getJarURI(clazz);
        resourceURI = getFile(uri, resourceName);

        return (resourceURI);
    }

    private static URI getJarURI(final Class<?> clazz)
        throws URISyntaxException
    {
        final ProtectionDomain domain;
        final CodeSource       source;
        final URL              url;
        final URI              uri;

        domain = clazz.getProtectionDomain();
        source = domain.getCodeSource();
        url    = source.getLocation();
        uri    = url.toURI();

        return (uri);
    }

    private static URI getFile(final URI    where,
                               final String fileName)
        throws ZipException,
               IOException
    {
        final File location;
        final URI  fileURI;

        location = new File(where);

        // not in a JAR, just return the path on disk
        if(location.isDirectory())
        {
            fileURI = URI.create(where.toString() + fileName);
        }
        else
        {
            final ZipFile zipFile;

            zipFile = new ZipFile(location);

            try
            {
                fileURI = extract(zipFile, fileName);
            }
            finally
            {
                zipFile.close();
            }
        }

        return (fileURI);
    }

    private static URI extract(final ZipFile zipFile,
                               final String  fileName)
        throws IOException
    {
        final File         tempFile;
        final ZipEntry     entry;
        final InputStream  zipStream;
        OutputStream       fileStream;

        tempFile = File.createTempFile(fileName.replace("/", ""), Long.toString(System.currentTimeMillis()));
        tempFile.deleteOnExit();
        entry    = zipFile.getEntry(fileName);

        if(entry == null)
        {
            throw new FileNotFoundException("cannot find file: " + fileName + " in archive: " + zipFile.getName());
        }

        zipStream  = zipFile.getInputStream(entry);
        fileStream = null;

        try
        {
            final byte[] buf;
            int          i;

            fileStream = new FileOutputStream(tempFile);
            buf        = new byte[1024];
            i          = 0;

            while((i = zipStream.read(buf)) != -1)
            {
                fileStream.write(buf, 0, i);
            }
        }
        finally
        {
            close(zipStream);
            close(fileStream);
        }

        return (tempFile.toURI());
    }

    private static void close(final Closeable stream)
    {
        if(stream != null)
        {
            try
            {
                stream.close();
            }
            catch(final IOException ex)
            {
                ex.printStackTrace();
            }
        }
    }
}
import java.io.Closeable;
导入java.io.File;
导入java.io.FileNotFoundException;
导入java.io.FileOutputStream;
导入java.io.IOException;
导入java.io.InputStream;
导入java.io.OutputStream;
导入java.io.UnsupportedEncodingException;
导入java.net.URI;
导入java.net.URISyntaxException;
导入java.net.URL;
导入java.net.url解码器;
导入java.security.CodeSource;
导入java.security.ProtectionDomain;
导入java.util.zip.ZipEntry;
导入java.util.zip.ZipException;
导入java.util.zip.ZipFile;
公共类文件
{
公共静态字符串getFileName(最终类所有者,
最终字符串名)
抛出一个异常,
ZipepException,
IOException
{
字符串文件名;
最终URI;
尝试
{
最终字符串外部;
最终解码的字符串;
最终int pos;
uri=getResourceAsURI(owner.getPackage().getName().replaceAll(“\\\”,“/”+“/”+name,owner);
external=uri.toURL().toExternalForm();
decoded=external;//urldecover.decode(外部,“UTF-8”);
pos=解码的索引(“:/”);
fileName=已解码的子字符串(pos+1);
}
捕获(最终文件NotFoundException ex)
{
fileName=null;
}
if(fileName==null | |!(新文件(fileName).exists())
{
fileName=getFileNameX(所有者,名称);
}
返回(文件名);
}
私有静态字符串getFileNameX(最终类clazz,最终字符串名)
抛出不支持的DencodingException
{
最终网址;
最终字符串文件名;
url=clazz.getResource(名称);
如果(url==null)
{
fileName=名称;
}
其他的
{
最终解码的字符串;
最终int pos;
decoded=urldecker.decode(url.toExternalForm(),“UTF-8”);
pos=解码的索引(“:/”);
fileName=已解码的子字符串(pos+1);
}
返回(文件名);
}
私有静态URI getResourceAsURI(最终字符串resourceName,
最后一节课(课堂)
抛出一个异常,
ZipepException,
IOException
{
最终URI;
最终URI资源URI;
uri=getJarURI(clazz);
resourceURI=getFile(uri,resourceName);
返回(resourceURI);
}
私有静态URI getJarURI(最终类clazz)
抛出URISyntaxException
{
最终保护域;
最终代码源;
最终网址;
最终URI;
domain=clazz.getProtectionDomain