I'm new to Java. I'm simply trying to build a .jar file of my applet so I can run it from my browser. This is what my directory structure looks like:
我是Java的新手。我只是尝试构建我的applet的.jar文件,以便我可以从浏览器运行它。这就是我的目录结构:
C:\java\pacman\src
contains all of the .java class files.
包含所有.java类文件。
C:\java\pacman\assets
contains about 4-5 images and audio files.
包含大约4-5个图像和音频文件。
If I try to use the following code:
如果我尝试使用以下代码:
Image someFile=getCodeBase().toString() + "file.png";
The result of getCodeBase()
is
getCodeBase()的结果是
file:/C:/java/pacman/bin/
However the following code fails to load:
但是,以下代码无法加载:
img=new ImgHelper(getCodeBase().toString() + "assets/");
ImageIO.read(new File(img.getPath("pacman.png")));
Moving my 'assets' folder to the 'bin' folder didn't fix this either. It tries loading:
将我的'assets'文件夹移动到'bin'文件夹也没有解决这个问题。它尝试加载:
file:/C:/java/pacman/bin/assets/pacman.png
saying:
Can't read input file!
But the url it gave opens fine if I paste it into run and hit enter:
但是如果我把它粘贴到run并点击回车,它给出的网址就打开了:
So to avoid myself a lot of headache i commented out the code in my ImgHelper class and did this:
所以为了避免自己很头疼,我在ImgHelper类中注释了代码,并做了这样的事情:
public ImgHelper(String dir)
{
//this.imgDir=dir;
imgDir="C:\\java\\pacman\\assets\\";
}
Which works perfectly. But I want to put this on a web server, and I have no idea how/what I should do to make all the images and sounds work. Any ideas?
哪个效果很好。但是我想把它放在网络服务器上,我不知道我应该怎样做什么才能让所有图像和声音都能正常工作。有任何想法吗?
Thanks...
3 个解决方案
#1
Why not put it all in a JAR file and then call Class.getResourceAsStream?
为什么不将它全部放在JAR文件中然后调用Class.getResourceAsStream?
A JAR file is better as it is a single HTTP connection rather than one HTTP connection per file. It is also much more flexible to use a Stream than a File.
JAR文件更好,因为它是单个HTTP连接,而不是每个文件一个HTTP连接。使用Stream比使用File更灵活。
getResourceAsStream will work when the files are not in a JAR as well, they need to be relative to the class file.
当文件不在JAR中时,getResourceAsStream也会工作,它们需要相对于类文件。
EDIT:
Another thing, the File method won't work if the applet is on a server as it will be trying to open the file from the local machine (I think, I haven't tried it) rather then from the server. Even if it tried to create a file path to the server that won't work.
另一件事,如果applet在服务器上,File方法将无法工作,因为它将尝试从本地机器打开文件(我想,我还没有尝试过),而不是从服务器打开。即使它试图创建一个无法正常工作的服务器的文件路径。
#2
I agree with tofubeer about the JAR, but if you want to put the image on your server, see the tutorial on Applet images here. The codebase will be whatever location your applet is on the server, and you can put images relative to that on the server as well. Use a media tracker along with the Applet.getImage() method to retrive the url. From the example:
我同意tofubeer有关JAR的信息,但如果您想将图像放在服务器上,请参阅此处的Applet图像教程。代码库将是您的applet在服务器上的任何位置,您也可以将相关的图像放在服务器上。使用媒体跟踪器和Applet.getImage()方法来检索URL。从示例:
my_gif = getImage(getDocumentBase(),"imageExample.gif");
#3
There are two possible solutions that would work:
有两种可行的解决方案可行:
- The images could be present outside the applet JAR. The applet could then be initialized with the location of the directory where the images are present. Once you have that information you could then load images from the server. The Sun Java tutorial provides an example usage of the applet parameter to pass the image source directory.
- The applet class loader could be utilized to load the images from the applet's JAR, using the getResourceAsStream() method.
图像可以存在于小程序JAR之外。然后可以使用存在图像的目录的位置来初始化applet。获得该信息后,您可以从服务器加载图像。 Sun Java教程提供了applet参数的示例用法,用于传递图像源目录。
可以使用applet类加载器使用getResourceAsStream()方法从applet的JAR加载图像。
PS: It would be helpful if you referred to the section in the Java tutorials to load icons for your application. The same section discusses a lot of the points brought forth by TofuBeer and John.
PS:如果您参考Java教程中的部分来加载应用程序的图标,将会很有帮助。同一部分讨论了TofuBeer和John提出的许多观点。
EDIT : The usage of the File API is not recommended because it ends up reading off the local file system. That is unacceptable for most users on the internet.
编辑:不建议使用File API,因为它最终会读取本地文件系统。这对于互联网上的大多数用户来说是不可接受的。
#1
Why not put it all in a JAR file and then call Class.getResourceAsStream?
为什么不将它全部放在JAR文件中然后调用Class.getResourceAsStream?
A JAR file is better as it is a single HTTP connection rather than one HTTP connection per file. It is also much more flexible to use a Stream than a File.
JAR文件更好,因为它是单个HTTP连接,而不是每个文件一个HTTP连接。使用Stream比使用File更灵活。
getResourceAsStream will work when the files are not in a JAR as well, they need to be relative to the class file.
当文件不在JAR中时,getResourceAsStream也会工作,它们需要相对于类文件。
EDIT:
Another thing, the File method won't work if the applet is on a server as it will be trying to open the file from the local machine (I think, I haven't tried it) rather then from the server. Even if it tried to create a file path to the server that won't work.
另一件事,如果applet在服务器上,File方法将无法工作,因为它将尝试从本地机器打开文件(我想,我还没有尝试过),而不是从服务器打开。即使它试图创建一个无法正常工作的服务器的文件路径。
#2
I agree with tofubeer about the JAR, but if you want to put the image on your server, see the tutorial on Applet images here. The codebase will be whatever location your applet is on the server, and you can put images relative to that on the server as well. Use a media tracker along with the Applet.getImage() method to retrive the url. From the example:
我同意tofubeer有关JAR的信息,但如果您想将图像放在服务器上,请参阅此处的Applet图像教程。代码库将是您的applet在服务器上的任何位置,您也可以将相关的图像放在服务器上。使用媒体跟踪器和Applet.getImage()方法来检索URL。从示例:
my_gif = getImage(getDocumentBase(),"imageExample.gif");
#3
There are two possible solutions that would work:
有两种可行的解决方案可行:
- The images could be present outside the applet JAR. The applet could then be initialized with the location of the directory where the images are present. Once you have that information you could then load images from the server. The Sun Java tutorial provides an example usage of the applet parameter to pass the image source directory.
- The applet class loader could be utilized to load the images from the applet's JAR, using the getResourceAsStream() method.
图像可以存在于小程序JAR之外。然后可以使用存在图像的目录的位置来初始化applet。获得该信息后,您可以从服务器加载图像。 Sun Java教程提供了applet参数的示例用法,用于传递图像源目录。
可以使用applet类加载器使用getResourceAsStream()方法从applet的JAR加载图像。
PS: It would be helpful if you referred to the section in the Java tutorials to load icons for your application. The same section discusses a lot of the points brought forth by TofuBeer and John.
PS:如果您参考Java教程中的部分来加载应用程序的图标,将会很有帮助。同一部分讨论了TofuBeer和John提出的许多观点。
EDIT : The usage of the File API is not recommended because it ends up reading off the local file system. That is unacceptable for most users on the internet.
编辑:不建议使用File API,因为它最终会读取本地文件系统。这对于互联网上的大多数用户来说是不可接受的。