ExceptionInInitializerError
The first line at the method getResourcePath cause exception/error, but only when it is called from a jar file.
Example, when I launch my project via eclipse, this method works fine. But when I export the project to a Runnable Jar File, I get error. I have no idea why.
Code:
public class Components extends JPanel
{
...
static java.io.File getResourcePath (String fileName)
{
File tmp = new File (FileHandler.fixURLAdress(Components.class.getResource (fileName).getFile ()));
// String outPath = tmp.toString ();
// if (outPath.startsWith("file:\\"))
// outPath = outPath.replace("file:\\", "");
//
// if (outPath.contains(".jar!"))
// outPath = outPath.replace(".jar!", ".jar");
return tmp;
}
}
Code:
public abstract class FileHandler
{
...
public static String fixURLAdress (String adress)
{
return adress.replace("%20", " ").replace("%5c", "");
}
}
Code:
java.lang.ExceptionInInitializerError
at server.gui.Main.main(Main.java:20)
Caused by: java.lang.NullPointerException
at server.gui.Components.getResourcePath(Components.java:271)
at server.gui.Components.<init>(Components.java:44)
at server.gui.MainWindow.<clinit>(MainWindow.java:25)
... 1 more
Re: ExceptionInInitializerError
You get a NullPointerException when you try and use a variable or other expression as if it had a non null value when it is really null. Typical uses that assume an expression is not null are accessing an array element with [] and accessing a method or instance variable with the dereference (dot) operator.
Code:
File tmp = new File (FileHandler.fixURLAdress(Components.class.getResource (fileName).getFile ()));
The most likely (only?) candidate here is "Components.class.getResource (fileName)". If this is null then .getFile() will fail with the reported exception. So it would be a good idea to check with System.out.println():
Code:
System.out.println("About to get file from " + Components.class.getResource (fileName));
File tmp = new File (FileHandler.fixURLAdress(Components.class.getResource (fileName).getFile ()));
If it proves to be null, then consult the API docs for getResource() is see the documented conditions under which that will happen.
Re: ExceptionInInitializerError
I understand what you mean. I still have trouble resolving this issue.
Added some debug stuff:
Code:
static java.io.File getResourcePath (String fileName)
{
java.net.URL url = Components.class.getResource ("files.obj");
if (url == null)
JOptionPane.showMessageDialog (null, "The URL is null", "Success!", JOptionPane.INFORMATION_MESSAGE); //Lets call this message1
else
JOptionPane.showMessageDialog (null, url.toString (), "Success!", JOptionPane.INFORMATION_MESSAGE); //message2
File file = new File (FileHandler.fixURLAdress(url.getFile()));
if (file == null)
JOptionPane.showMessageDialog (null, "The file is null", "Success!", JOptionPane.INFORMATION_MESSAGE); //message3
JOptionPane.showMessageDialog (null, "File exists: " + file.exists(), "Success!", JOptionPane.INFORMATION_MESSAGE); //message4
JOptionPane.showMessageDialog (null, file.toString(), "Success!", JOptionPane.INFORMATION_MESSAGE); //message5
System.exit (0);
return null;
}
So I export this project to a JAR. Then I place files.obj in the same directory as Components.class.
So when I launch this, I get message2, which means the method getResource does not return null. The url.toString () look like this: jar:file:/C:/Users/Pojahn/Desktop/ee.jar!/server/gui/files.obj
Then is message4 displayed and writes "false"(i e the file does not exist). Then message5 display this: file:\C:\Users\Pojahn\Desktop\ee.jar!\server\gui\f iles.obj
Why does java tell that the file not exist when it do?
Re: ExceptionInInitializerError
I just found out that you can not get an File instance of a file inside a jar. java - Create a file object from a resource path to an image in a jar file - Stack Overflow
So everything in this thread is obsolete.
What I was trying to to is read and write data to a file inside a jar. I know how to read from a jar, but I dont know how to write text/data to a jar file. Anyone know?
Re: ExceptionInInitializerError
It is important to realise that when we make a jar file we take files and turn them into jar entries. Entries in a jar file look a bit like files in a directory, but they are not: they are a different thing.
Quote:
So when I launch this, I get message2, which means the method getResource does not return null. The url.toString () look like this: jar:file:/C:/Users/Pojahn/Desktop/ee.jar!/server/gui/files.obj
Then is message4 displayed and writes "false"(i e the file does not exist). Then message5 display this: file:\C:\Users\Pojahn\Desktop\ee.jar!\server\gui\f iles.obj
Why does java tell that the file not exist when it do?
You get message 2 "Success" because the URL jar:file:/C:/Users/Pojahn/Desktop/ee.jar!/server/gui/files.obj really exists. It is an entry in the ee.jar file.i
You get message 4 "File exists: false" because the file file:\C:\Users\Pojahn\Desktop\ee.jar!\server\gui\f iles.obj does not exist.
-----
You have not said what you intend doing with file, but, whatever it is, a File is the wrong sort of thing to be working with if you what you have is a jar entry. Many Java library classes have two forms of method: one taking a File argument and another taking a URL - precisely for dealing with this sort of situation. You can load an image for example using either a File (if it is a real existing file) or a URL (if it is an entry in a jar).
Re: ExceptionInInitializerError
Our posts crossed!
I don't think you can write data to a jar file while it is being used (for the application's classes). It is better to put the data somewhere else: a well known location (like somewhere in the user's home directory), some specific data location to which the user has access, a database, etc.