简体   繁体   English

部署 java 小程序时遇到很多麻烦

[英]Having a lot of trouble deploying a java applet

I'm new to Java.我是 Java 的新手。 I'm simply trying to build a.jar file of my applet so I can run it from my browser.我只是想构建我的小程序的一个.jar 文件,以便我可以从我的浏览器运行它。 This is what my directory structure looks like:这是我的目录结构的样子:

C:\java\pacman\src

contains all of the.java class files.包含所有.java class 文件。

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.将我的“assets”文件夹移动到“bin”文件夹也没有解决这个问题。 It tries loading:它尝试加载:

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:但是如果我将它粘贴到运行中并按回车键,它给出的 url 会很好地打开:

So to avoid myself a lot of headache i commented out the code in my ImgHelper class and did this:所以为了避免让自己很头疼,我在我的 ImgHelper class 中注释掉了代码并这样做了:

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.但我想把它放在 web 服务器上,我不知道我应该如何/做什么才能使所有图像和声音正常工作。 Any ideas?有任何想法吗?

Thanks...谢谢...

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. JAR 文件更好,因为它是单个 HTTP 连接,而不是每个文件一个 HTTP 连接。 It is also much more flexible to use a Stream than a File.使用 Stream 也比使用文件灵活得多。

getResourceAsStream will work when the files are not in a JAR as well, they need to be relative to the class file.当文件不在 JAR 中时,getResourceAsStream 也可以工作,它们需要与 class 文件相关。

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.另一件事,如果小程序在服务器上,则 File 方法将不起作用,因为它将尝试从本地计算机(我想,我没有尝试过)而不是从服务器打开文件。 Even if it tried to create a file path to the server that won't work.即使它试图创建一个到服务器的文件路径也不起作用。

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 .我同意 tofubeer 关于 JAR 的看法,但如果您想将图像放在服务器上,请参阅此处的 Applet 图像教程。 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.使用媒体跟踪器和 Applet.getImage() 方法来检索 url。 From the example:从例子:

 my_gif = getImage(getDocumentBase(),"imageExample.gif");

There are two possible solutions that would work:有两种可行的解决方案:

  • The images could be present outside the applet JAR.图像可能存在于小程序 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. Sun Java 教程提供了小程序参数传递图像源目录的示例用法
  • The applet class loader could be utilized to load the images from the applet's JAR, using the getResourceAsStream() method.小程序 class 加载程序可用于使用 getResourceAsStream() 方法从小程序的 JAR 加载图像。

PS: It would be helpful if you referred to the section in the Java tutorials to load icons for your application . PS:如果您参考Java 教程中的部分来为您的应用程序加载图标,将会很有帮助。 The same section discusses a lot of the points brought forth by TofuBeer and John .同一部分讨论了TofuBeerJohn提出的许多观点。

EDIT: The usage of the File API is not recommended because it ends up reading off the local file system.编辑:不推荐使用文件 API,因为它最终会读取本地文件系统。 That is unacceptable for most users on the internet.对于互联网上的大多数用户来说,这是不可接受的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM