get path of image file that is outside the project in java
-
I use this code to display image that locates outside the my java project, but I got NullPointerException every time and I can only use images that are inside my project directory. why ? Icon welcomeImg = new ImageIcon(getClass().getResource("D:/img/welcome.png")); or Icon welcomeImg = new ImageIcon(getClass().getResource("D://img/welcome.png")); JLabel welcomingLb = new JLabel(welcomeImg);
-
Answer:
You do not need to use the http://docs.oracle.com/javase/6/docs/api/java/lang/ClassLoader.html class to access your file since you are giving its full path. Try instead : Icon welcomeImg = new ImageIcon("D:/img/welcome.png"); Source : Javadoc of http://docs.oracle.com/javase/1.4.2/docs/api/javax/swing/ImageIcon.html#ImageIcon%28java.lang.String%29
sasola at Stack Overflow Visit the source
Other answers
http://docs.oracle.com/javase/6/docs/api/java/lang/Class.html#getResource%28java.lang.String%29 expects the resource to be on the classpath. If you want to read from a random file, use a http://docs.oracle.com/javase/6/docs/api/java/io/File.html, or use the http://docs.oracle.com/javase/6/docs/api/javax/swing/ImageIcon.html#ImageIcon%28java.lang.String%29.
Dave Newton
See: http://stackoverflow.com/questions/2343187/loading-resources-using-getclass-getresource Basically when you use getResource it is expecting the file to be on the Classpath. There is also a constructor for ImageIcon that takes a String filename, so you can pass the path to the Icon file directly. Check out the http://docs.oracle.com/javase/7/docs/api/javax/swing/ImageIcon.html#ImageIcon%28java.lang.String%29
DaveJohnston
You can try using a File, like so: File f = new File("C:\\folder\\stuff\\MyFile.class");
f1dave
Related Q & A:
- Where did a downloaded file from SharePoint come from? (it's SharePoint path?Best solution by SharePoint
- How to upload file to google cloud storage using Java?Best solution by Stack Overflow
- How to Read xml file in java?Best solution by Stack Overflow
- What is a great idea for an Android or Java project?Best solution by 1000projects.org
- Where can I get a good online file storage?Best solution by dropbox.com
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.