简体   繁体   English

运行.jnlp文件时Java图像混乱

[英]Java Image messes up when I run the .jnlp file

I have a method that gets called at the start of my program, and it works fine when I run the jar, but when I run the jnlp file it crashes. 我有一个在程序开始时被调用的方法,当我运行jar时,它工作正常,但是当我运行jnlp文件时,它崩溃了。

public void newImg(String i)
{
    try
    {
        File file = new File(i);
        img = ImageIO.read(file);
    }
}

img is a bufferedImage, by the way. 顺便说一句,img是bufferedImage。

Odds are the path you're giving to the file, or permissions, don't match. 赔率是您赋予文件或权限不匹配的路径。 Put the File ctor into a try block, catch the exception, and find out what it is. 将File ctor放入try块,捕获异常,然后找出其含义。

It should look something like this: 它看起来应该像这样:

try {
    File file =  new File(i);
    img = ImageIO.read(file);
} catch (Exception ex) {
    // You probably want to open the java console, or use a logger
    // as a JNLP may send stderr someplace weird.
    Systemm.err.println("Exception was: ", ex.toString());
}

The code you have doesn't do anything with the exception. 您拥有的代码对异常没有任何作用。

You might want to look at the exceptions thread in the Java Tutorial. 您可能需要查看Java教程中的异常线程

update 更新

See my comments. 查看我的评论。 I just tried something and confirmed what I was thinking -- code with a try {} and no catch or finally won't even compile. 我只是尝试了一下,然后确认了我的想法-使用try {}编码,没有任何catch甚至finally都无法编译。 if this is really the code you think you're working with, you've probably been loading an old class file; 如果这确实是您认为正在使用的代码,则可能是您正在加载旧的类文件; this one hasn't compiled. 这个还没有编译。

$ cat Foo.java 
public class Foo {
     public void tryit() {
         try {
             File f = new File(null);
         }
     }
}
$ javac Foo.java
Foo.java:3: 'try' without 'catch' or 'finally'
         try {
         ^
1 error
$ 

Maybe your not loading your image properly. 也许您没有正确加载图像。 Don't use the relative location of the file. 不要使用文件的相对位置。 This will be different for each OS. 对于每个操作系统,这将是不同的。 Your image in the JAR you should be loaded correctly like this: 您在JAR中的图片应该像这样正确加载:

URL url = this.getClass().getResource("image.jpg");
Image img = Toolkit.getDefaultToolkit().getImage(url);

This will load a file called image.jpg that is located in the same location as the class. 这将加载一个名为image.jpg的文件,该文件与该类位于相同的位置。 You can also use things like File.pathSeparator if its in another location. 您也可以在其他位置使用File.pathSeparator之类的东西。

Use one of these two methods to load it as a resource: 使用以下两种方法之一将其加载为资源:

http://java.sun.com/javase/6/docs/api/java/lang/Class.html#getResource(java.lang.String) http://java.sun.com/javase/6/docs/api/java/lang/Class.html#getResourceAsStream(java.lang.String) http://java.sun.com/javase/6/docs/api/java/lang/Class.html#getResource(java.lang.String) http://java.sun.com/javase/6/docs/ API /爪哇/郎/ Class.html#的getResourceAsStream(java.lang.String中)

Make sure you have the correct file name/path. 确保您具有正确的文件名/路径。

Make sure you have file access to the file system. 确保您有权访问文件系统。

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

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