0

I have a sqlite database file that i am accessing using a swing aplication. The jdbc driver and everyhting works. If i get the exact file location and paste it into the database url it will work. I tried using a relative file path and it does not work. I created a method to get the direct file path to the working directory of the program and craeted a map if you will of the folder and its contents. it should work, but doesn't, i was wondering if anyone could tell me why. here is the method

    public String GetAbsPath(){
     File workingDir=new File(".");
  String absolute=workingDir.getAbsolutePath();
 char[] absA=absolute.toCharArray();

  ArrayList<String> list=new ArrayList<>();
        for (int x = 0; x < absA.length; x++) {
            String listPiece = Character.toString(absA[x]);
             list.add(listPiece);
        }
     StringBuilder result = new StringBuilder();
        for (int i = 0; i < list.size()-1; i++) {
            if(list.get(i).equals("\\")){
                list.set(i, "\\\\");
            }
            result.append(list.get(i));
        }
        String path=result.toString();

    return path;
}

it returns the exact file path that i enter in manually, but will not find the file. both methods work with netbeans but only entering in the exact file path into a string works when i try to run the file outside of netbeans. This is where the method is called.

 GetPath dir=new GetPath();
        String dirFilePath = "jdbc:sqlite:"+dir.GetAbsPath()+"Office.sqlite";
        database=dirFilePath;

This worked...

       CodeSource codeSource = MainFrame.class.getProtectionDomain().getCodeSource();
            File jarFile=null;
            String jarDir=null;
            try {
                jarFile = new File(codeSource.getLocation().toURI().getPath());
                jarDir = jarFile.getParentFile().getPath();
            } catch (URISyntaxException ex) {
                Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex);
            }

      database ="jdbc:sqlite:"+jarDir +"\\Office.sqlite";
derek
  • 123
  • 2
  • 4
  • 15
  • 1
    See this link:[How to get realative path for database?](http://stackoverflow.com/questions/17978152/how-to-get-realative-path-for-database/17978559#17978559) – Azad Aug 10 '13 at 21:27
  • I don't think you need to be escaping the '\' – MadProgrammer Aug 10 '13 at 21:27
  • 1) A single blank line of white space in source code is *always* enough. Blank lines after `{` or before `}` are also typically redundant. 2) Use a consistent and logical indent for code blocks. The indentation of the code is intended to help people understand the program flow. 3) Please learn common [Java naming conventions](http://java.sun.com/docs/books/jls/second_edition/html/names.doc.html#73307) (specifically the case used for the names) for class, method & attribute names & use them consistently. – Andrew Thompson Aug 11 '13 at 05:06
  • `File workingDir=new File(".");` This is probably not pointing where you expect. Do some basic debugging by printing the absolute path. – Andrew Thompson Aug 11 '13 at 05:08
  • I thought of that. I've debugged. Printed the absolute file path. If you look below I use system resourses to get the working directory and append it with the file name. The string I used for the path works, the appended system resource does not. And a boolean statement one.equals(theOther) returns true. – derek Aug 11 '13 at 16:15
  • @derek I made an additional edit. – Steve P. Aug 11 '13 at 20:00

1 Answers1

2

To get the absolute path of the current working directory use:

 System.getProperty("user.dir")

which returns a String.

EDIT:
If you want to replace the "\" with a "/", use this:

String cwd = System.getProperty("user.dir").replaceAll("\\\\", "/");

or

String cwd = System.getProperty("user.dir").replaceAll(Pattern.quote("\\"), "/");

For an explanation of the usage of the regex in the latter two statements, see here.

EDIT 2 (based off of comments):
Since the comparison of the two strings via .equals() return true, the only thing that I can think of is that this is an odd NetBeans issue. I suggest trying a different IDE, perhaps Eclipse.

Community
  • 1
  • 1
Steve P.
  • 14,489
  • 8
  • 42
  • 72
  • that doesnt work either. i originally did it this way because it wouldnt work without escaping the backslash. – derek Aug 10 '13 at 21:42
  • the other post says to use / so there is no need for the \\ but that doesnt work either...ive been struggling with this since the beginning of the project. I put a file chooser in the runnable of the main method so the user can select the db file before opening the main window, but this is not ideal for "idiot proofing" the program. – derek Aug 10 '13 at 21:50
  • nope, these methods are much more efficient ways of accomplishing what i did with my method, however it will not connect outside of netbeans. Thank you for the help, i will continue playing with it and hopefully something works for me. – derek Aug 10 '13 at 22:01
  • btw one last shot, maybe something will click...i can literally copy paste the output from these methods into a string literal and place the string into the database variable and it runs... – derek Aug 10 '13 at 22:04
  • @derek Copy and paste whatever is working and compare the two strings via `.equals()`, if that returns `true`, then obviously the problem is elsewhere. If it returns false, prints the strings character by character via `stringName.toCharArray()` to see what the difference is. If the problem is elsewhere, perhaps this [page](https://bitbucket.org/xerial/sqlite-jdbc#HowtoSpecifyDatabaseFiles) may help you. – Steve P. Aug 10 '13 at 22:15
  • ok thank you for the suggestion. ill be sure and update this post if and when i find the problem – derek Aug 10 '13 at 22:17
  • i edited with the fix...the "user.dir" was not returning the same thing when running outside of netbeans. – derek Aug 15 '13 at 02:35