java security not allowing me to open take pics using Jfilechooser - java

I am making an application in which one of the features the constructor requires a picture. The picture is selected using the JFileChooser obviously and then displayed on a JLabel. My problem is I do not have security privilege to access the picture. I tested to see if I do get the absolute path and if the file exists and I did get the path and true for the latter. So how do I give my app access to at least just get pictures?
My code
JFileChooser chooser = new JFileChooser();
chooser.setFileFilter(new FileNameExtensionFilter("jpg","png"));
int returnVal = chooser.showOpenDialog(diag);
if(returnVal == JFileChooser.APPROVE_OPTION) {
weaponImg = new ImageIcon(TempDialogs.class.getResource(chooser.getSelectedFile().getAbsolutePath()));
weaponPic.setIcon(weaponImg);
weaponPic.revalidate();
weaponPic.repaint();
My error
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at javax.swing.ImageIcon.<init>(Unknown Source)
at ui.TempDialogs$5.mouseClicked(TempDialogs.java:171)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$500(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)

java security not allowing me ..
This has nothing to do with security, despite the stack trace mentioning 'security' in some of the lines. The real problem is at the very top of the stack trace, ..
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
And I expect it all starts with this line of code..
weaponImg = new ImageIcon(TempDialogs.class.getResource(chooser.getSelectedFile().getAbsolutePath()));
Which is both wrong and unnecessarily convoluted.
Get resource is for producing an URL from resources on the application's class path, it is neither needed nor useful for files.
So in this case of trying to access a file, we can use either a File object, or a String the represents a path to a file on the file system. So it could be shortened to:
weaponImg = new ImageIcon(chooser.getSelectedFile().getAbsolutePath()); // use String
But as alluded to, it could also be loaded as a plain File, so this would also work:
weaponImg = new ImageIcon(chooser.getSelectedFile()); // use File!

Related

Java: Invalid thread access - pop up menu

Within an eclipse plugin I am trying to create a pop up window that activates within an editor and adds syntax to a string depending on the selection chosen in the pop up window. I have an editor class that extends AbstractTextEditor and within it I am trying to use a keylistener to pop up a window that contains a list of string manipulation functions and then when an item in the list is selected I want to replace some selected content with the new string returned from the method in the pop up window. When I attempt to do this (with the code in the 2 linked files), I receive the error below:
Exception in thread "AWT-EventQueue-0" org.eclipse.swt.SWTException:
Invalid thread access
at org.eclipse.swt.SWT.error(SWT.java:4595)
at org.eclipse.swt.SWT.error(SWT.java:4510)
at org.eclipse.swt.SWT.error(SWT.java:4481)
at org.eclipse.swt.widgets.Widget.error(Widget.java:451)
at org.eclipse.swt.widgets.Widget.checkWidget(Widget.java:369)
at org.eclipse.swt.custom.StyledText.getSelectionRange(StyledText.java:4743)
at githubflavoredmarkdowneclipseplugin.MarkdownEditor.test(MarkdownEditor.java:140)
at autocomplete.AutoComplete$1.keyPressed(AutoComplete.java:50)
at java.awt.AWTEventMulticaster.keyPressed(Unknown Source)
at java.awt.Component.processKeyEvent(Unknown Source)
at javax.swing.JComponent.processKeyEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.KeyboardFocusManager.redispatchEvent(Unknown Source)
at java.awt.DefaultKeyboardFocusManager.dispatchKeyEvent(Unknown Source)
at java.awt.DefaultKeyboardFocusManager.preDispatchKeyEvent(Unknown
Source)
at java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(Unknown
Source)
at java.awt.DefaultKeyboardFocusManager.dispatchEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$500(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown
Source)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown
Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown
Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
It seems that when autoupdate class tries to inform the editor class that a change has been selected it cannot manipulate the styledText variable. Does anyone know how to fix this?
The two files that contain this code can be reviewed at these links:
http://www.filedropper.com/markdowneditor
http://www.filedropper.com/autocomplete
The reason you are getting that exception is because when you make changes that are UI related with SWT you have to execute a runnable vis Display.syncExec(myRunable) otherwise that exception will occur.

FileUtils.copyURLToFile not working for project file

I am building Java SWT application. As a part of whole process i am working on an excel file which have specific format to process things. What i want is a link on my JFrame which triggers download of a sample excel file stored in my project without local or internet dependency. I don't know how exactly i can do this.
I have tried this FileUtils.copyURLToFile but it is throwing null pointer exception. I am showing my code. Any help would be greatly appreciated.
public void actionPerformed(ActionEvent arg0) {
log("clicked to download");
String temp_path = System.getProperty("user.dir")+"\\resources\\EmailSheet.xlsx";
log("path = " + temp_path);
URL inputUrl = getClass().getResource(temp_path);
log("URL = " +inputUrl.toString());
File dest = new File("D:/new_file.xlsx");
try {
FileUtils.copyURLToFile(inputUrl, dest);
} catch (IOException e) {
log("Error saving sample file : " + e.getMessage().toString());
e.printStackTrace();
}
}
My Console is printing this error
Mon Oct 03 09:14:29 IST 2016 : clicked to download
Exception in thread "AWT-EventQueue-0" Mon Oct 03 09:14:29 IST 2016 : path = \resources\EmailSheet.xlsx
java.lang.NullPointerException
at tech.excelemail.com.TechExcelEmailApp$7.actionPerformed(TechExcelEmailApp.java:221)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$500(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
I want to know if there is mistake in my code. or if i am going the wrong way. Ask me anything that if u think something missing unspecified.
Thanks in advance.
It's rather hard to believe that System.getProperty("user.dir") really returned an empty string, as your trace appears to suggest. Is this the real code?
In any case resources are named relative to the CLASSPATH, not the file system, so user.dir has nothing to do with it. They aren't files. And they don't use backslashes. Use forward slashes. And make sure that the resource named is present under that name in the JAR file.

Java Load PDF File File Doesnt Exist

I am attempting to have my Java Application open a PDF file when the user clicks button. However, I get the stack trace below stating that the file doesn't exist. Bascially I would like to be able to load this file when the user makes the selection.
Below I will have the stack trace then the code and a screenshot of the path.
StackTrace:
Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: The file: \RFBase-TD_Communications\src\pdf\RFTDAnalyzerHelpFile.pdf doesn't exist.
at java.awt.Desktop.checkFileValidation(Unknown Source)
at java.awt.Desktop.open(Unknown Source)
at GUI.rfbgui.openPDF(rfbgui.java:787)
at GUI.rfbgui.access$7(rfbgui.java:773)
at GUI.rfbgui$6.actionPerformed(rfbgui.java:921)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.AbstractButton.doClick(Unknown Source)
at javax.swing.plaf.basic.BasicMenuItemUI.doClick(Unknown Source)
at javax.swing.plaf.basic.BasicMenuItemUI$Handler.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$200(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
Code:
private static void openPDF()
{
File pdfHelpFile = new File("/RFBase-TD_Communications/src/pdf/RFTDAnalyzerHelpFile.pdf");
try
{
Desktop.getDesktop().open(pdfHelpFile);
}catch(IOException ex)
{
ex.printStackTrace();
}
}
I have some general advice for how to handle these situations. Files were one of the things I got very frustrated with when starting to learn to program.
Use System.getProperty("user.dir"); This can be very helpful especially when you do not know where the program is going to be run from, or you have a specific file structure.
In Java, I generally recommend using "\" instead of "/".
Run a sanity check on the file you are attempting to load. Specifically check if it is null, .isFile(), etc. You never know what you might get back, so its good to take a peak before accidently crashing your program.
Here is some links for similar questions that might help you out;
How should I load files into my Java application?
Getting the Current Working Directory in Java
Getting the inputstream from a classpath resource (XML file)
File myFile = new File(getClass().getResource("/files/test.pdf").toURI());
or
if (Desktop.isDesktopSupported()) {
try {
File myFile = new File("/path/to/file.pdf");
Desktop.getDesktop().open(myFile);
} catch (IOException ex) {
// no application registered for PDFs
}

Works in Netbeans, but not "outside" of it

This code works perfect when I press PLAY in netbeans, but when I build it and start the Jar file it will not.
private void SettingsMouseEntered(java.awt.event.MouseEvent evt) {
ImageIcon Setting = new ImageIcon(getClass().getResource("/pic/settings-icon.png"));
Settings.setIcon(Setting);
HooverBar.setText("Settings");
}
private void SettingsMouseExited(java.awt.event.MouseEvent evt) {
ImageIcon Setting = new ImageIcon(getClass().getResource("/pic/settings-icon-half.png"));
Settings.setIcon(Setting);
HooverBar.setText("");
}
private void SettingsMouseClicked(java.awt.event.MouseEvent evt) {
CardLayout card = (CardLayout)mainPanel.getLayout();
card.show(mainPanel,"gui5");
}
I can not figure out what I have done?
When i start the with "Java -jar Knowhow.jar" and move the mouse over the "settings" i get this error
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at javax.swing.ImageIcon.<init>(Unknown Source)
at AppPackage.GUI.SettingsMouseEntered(GUI.java:1755)
at AppPackage.GUI.access$1200(GUI.java:26)
at AppPackage.GUI$11.mouseEntered(GUI.java:278)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEnterExit(Unknown Source)
at java.awt.LightweightDispatcher.trackMouseEnterExit(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$500(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
Your images are not packaged within your jar. You need to change that.
I can't tell from the details of your question why that happens but the context is this:
getClass().getResource("/pic/settings-icon.png")
is loading the image from your classpath. That is from each element of the classpath. So your NetBeans classpath is different from the one of your java -jar command.
Have a look at the run configuration in your NetBeans to see your classpath there and map the classpath of your command to it.

getResourceAsStream() returning null for properties file

I try to load a file named config.properties in the package config.
A snippet of my code in Main.java:
//Read config.properties
Properties properties = new Properties();
System.out.println(Main.class.getClassLoader().getResource("config/config.properties").toString());
InputStream propertiesFile = Main.class.getClassLoader().getResourceAsStream("config/config.properties");
properties.load(propertiesFile);
This however gives me a NullPointerException. But when I load img/background/background.png I use: (from Panel.java)
background = new ImageIcon(this.getClass().getClassLoader().getResource("img/background/background.png")).getImage();
This works fine. I've read a lot of questions on stackoverflow already, but can't find a solution to my problem. I do not see the difference between the loading of the background image or the properties file, other than the fact that the properties file is loaded in a static context. But as far as I can see, this should work.
What am I forgetting?
EDIT: I just ran System.out.println(Main.class.getClassLoader().getResource("config/config.properties").toString());, which printed the correct path to config.properties.
Stacktrace: java.lang.NullPointerException
at main.Main.startGame(Main.java:70)
at main.gui.panel.MenuPanel$1.actionPerformed(MenuPanel.java:31)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$400(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
Main.java - line 70:
int maxFPS = Integer.getInteger(properties.getProperty("FPS"));
config.properties:
FPS=45
fpsCap=1
The problem wasn't with the getResourceAsStream("config/config.properties"), but with the way I read the properties. Property FPS is written as a String, to make this in an int I had to use Integer.parseInt(), instead of Integer.getInteger().
Try getting it from the current thread context loader:
Properties properties = new Properties();
ClassLoader loader = Thread.currentThread().getContextClassLoader();
try (InputStream in = loader.getResourceAsStream("config/config.properties")) {
properties.load(in);
} catch (IOException e) {
throw new IllegalStateException("Cannot start, properties not found.");
}
This code compiles for JDK 7+, since it uses auto closeable, but can be translated to previous JDK version by moving the resource loader statement to be in in the try-catch clause.

Categories

Resources