0

I am trying to access a file using the class path like so:

String path = getClass().getProtectionDomain().getCodeSource()
            .getLocation().toString();
    File test = new File(path);
    File table = new File(test, "testFile.xlsx");

I am doing this because I need to create a Jar that will read and write to this file if it is in the same folder.

I get this error:

java.io.FileNotFoundException:  "myFilepath" (The filename, directory name, or volume label syntax is incorrect)

If I copy and paste myFilepath in a file browser it brings up my file. Anyone see what I am doing wrong, or ways I can improve my methods?

PandaBearSoup
  • 699
  • 3
  • 9
  • 20
  • you need to clarify more your question, add more context and explain the intention, not only the actual 'thing' that needs to be done. (or I will downvote you) – David Hofmann Jul 18 '13 at 21:18
  • @DavidHofmann I access an excel spreadsheet. I edit the contents of this spreadsheet. I save the changes to this spreadsheet to the original file. This application is for a non-developer. He needs to be able to run the executable and then open the spreadsheet to look at the data. This is all the context, instead of down/up voting I would greatly appreciate some insight. – PandaBearSoup Jul 18 '13 at 21:31
  • So you java swing application that you distribute in a jar. That application should open a file in the file system where it is loaded, modify the file and save it. Is that it ? – David Hofmann Jul 19 '13 at 11:09

2 Answers2

1

Instead of trying to get the class path name, Create a File object and then get its absolute path using the getAbsolutePath() method. This will give the path of the source file which runs the code.

Kindly try the below code:-

 java.io.File f = new java.io.File("H");
        String path;
        path = f.getAbsolutePath();
        path = path.substring(0, (path.length() - f.getName().length()));
        f.deleteOnExit();

where the string path will then contain your class file directory path.

  • http://stackoverflow.com/questions/2837263/how-do-i-get-the-directory-that-the-currently-executing-jar-file-is-in I tried this method and got the same error. – PandaBearSoup Jul 18 '13 at 20:37
  • Kindly try the above code and see if you get your absolute class file path without the class name with the "path" variable. – G.Srinivas Kishan Jul 18 '13 at 20:42
  • 1
    This brought me to the project folder, the file is contained within src/main/resources. How would I make this work when having the file in the same folder as the jar? – PandaBearSoup Jul 18 '13 at 20:46
  • kindly try adding the jar file folder name with this path thru concatenation and then use the new directory and see if it works. – G.Srinivas Kishan Jul 18 '13 at 20:51
  • Then the compiled class file is the same as that of jar file so the class file path is reached, with the jar file in it. – G.Srinivas Kishan Jul 18 '13 at 20:58
  • Once you create a jar file then the jar file path and then the class path will be the same. So you could be able to edit files using that jar file containing the class file with the code given above. – G.Srinivas Kishan Jul 18 '13 at 21:05
  • I do not get how the above code is getting the file and I still get errors. You create the path but then you do nothing with the path to get the file. Can you elaborate? – PandaBearSoup Jul 18 '13 at 21:08
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/33733/discussion-between-g-srinivas-kishan-and-pandabearsoup) – G.Srinivas Kishan Jul 19 '13 at 01:54
0

You can't access a File in your classpath, but you can get a resource stream from your classpath and read the contents from it. You need to look at this answer https://stackoverflow.com/a/1464366/39998

Community
  • 1
  • 1
David Hofmann
  • 5,683
  • 12
  • 50
  • 78
  • 1
    If I access the file this way as you suggested: InputStream stream = Test.class.getResourceAsStream("/SomeTextFile.txt"); How do I then create a FileOutputSteam to write the changes to this file? – PandaBearSoup Jul 18 '13 at 20:42
  • Normally you read the contents of some file inside your jar (that is what we call a resource). What exactly you want to do afterwards, modify the file inside the jar ? – David Hofmann Jul 18 '13 at 20:48
  • This file needs to be accessed after executing the program. – PandaBearSoup Jul 18 '13 at 20:53