0

I have java app, where I copy directories (with subdir and files) to SDCard. I wanted to save these dirs with project. In eclipse everything works fine. But, when I export project to runnable jar, app is not able to find or copy these dirs. No error is showed.

File folder2 = new File(getClass().getResource("/urgent/").getFile());
File folder4 = new File(getClass().getResource("/service/").getFile());

The path, I get from jar app, when I want to copy dir is:

jar:file:\C:\path\to\jar\file\myapp.jar!\urgent

project hierarchy:

-project
 --src
 --build
 --urgent
  ---subdir
 --service
  ---subdir

Is it possible to work with your own directories in jar and how?

Haniku
  • 671
  • 1
  • 7
  • 16
  • 1
    If you want to use directories, then you shouldn't be using `getResource()`... `getResource()` is for getting things from the class path, which may not be directories. If you want to use directories, you should be using `File` or `Path`, etc. – Mark Rotteveel Aug 21 '18 at 11:31
  • https://stackoverflow.com/questions/320542/how-to-get-the-path-of-a-running-jar-file – James Jithin Aug 21 '18 at 11:34

1 Answers1

0

You are trying to work on resources on you classpath, a random folder on your harddrive is not in it, so you cannot find it.

What you need to do is: 1. Work with parameters from the commandline (see how to use args[] in your main method) 2. use those arguments to parametrize your custom copy mehod

or:

Try using the copyDirectory(File srcDir, File destDir) method from the Apache Commons IO library instead. To use this reearch how to include libraries in your project, preferably by using maven.

fl0w
  • 3,593
  • 30
  • 34