Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

How to set relative paths in Java

mohniAug 28 2012 — edited Aug 30 2012
I have tried to set relative path in Netbeans as
String fileName=getClass().getResource("./resources/folder/abc.xml").toString().substring(6);
This works properly when i run class file but, it does'nt work for jar file, the jar file does'nt run.
It gives an Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException in main method
public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new MyProject().setVisible(true);     //Exception on this line
            }
        });
    }
I also used this code:
File Currentdir=new File(".");
        try{
           String CurrentWorkingDir=Currentdir.getCanonicalPath();
        } catch(Exception e){System.out.println(e);}

File file =new File(CurrentWorkingDir+"./resources/folder/abc.xml");
but in File it gives the same Exception

if i use getCanonicalPath() in String as:
String fileName=CurrentWorkingDir+./resources/folder/abc.xml"; //CurrentWorkingDir=Currentdir.getCanonicalPath();{code}
it works properly

Is there any way to set relative path so it works in any situation.

Edited by: mohni on Aug 28, 2012 10:25 AM

Edited by: mohni on Aug 28, 2012 10:26 AM                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Sep 27 2012
Added on Aug 28 2012
4 comments
2,812 views