0

I have a program which copies files from itself (inside the .jar) outside, but whenever I try to run it from Eclipse, it gives me an exception:

java.io.FileNotFoundException: <my workspace>\<my project>\bin (Access is denied)
    at java.util.zip.ZipFile.open(Native Method)
    at java.util.zip.ZipFile.<init>(Unknown Source)
    at java.util.jar.JarFile.<init>(Unknown Source)
    at java.util.jar.JarFile.<init>(Unknown Source)

I'm guessing this is because Eclipse doesn't make a .jar every time you run. I can run my program perfectly by exporting the runnable jar, but it's tedious and painful to export every time I want to test the program.

Is there any way you can tell Eclipse to make a .jar every time it runs (I know it will be slow, but that's fine). Or at least emulate a jar file?

  • 1
    Looks like a re-post of [this question](http://stackoverflow.com/questions/1062941/build-project-into-a-jar-automatically-in-eclipse) – Salain Aug 20 '12 at 01:01
  • @Salain Sort of, but not exactly. That question asks whether you can have the jar updated every time you build. What I want is to run the jar immediately when you build (I don't know anything about Apache Ant, but it [might not work in my case](http://stackoverflow.com/a/9033069/837703)). The other answers would make the process easier though. –  Aug 20 '12 at 14:32
  • 1
    I see. Well if you want to test the .jar why don't you follow this protocol to test it: export from Eclipse as a .jar, run in shell with "java -jar myProject.jar". – Salain Aug 20 '12 at 14:47
  • @Salain Yes, that's what I'm doing. Everything's fine and going okay, just wanting to know if you can press the run button, or doble-slick something and Eclipse will make the jar and automatically run it. It's working, just kind of painful when you have to do it a lot. –  Aug 20 '12 at 14:57

1 Answers1

4

@Salain is right - but if your use case is just copying files outside of your project, wouldn't it be easier to ignore whether they're in a jar or not, and just get a handle on them via YourClass.class.getResourceAsStream("/path/to/file")?

ryanp
  • 4,905
  • 1
  • 30
  • 39
  • That would be much easier than all the zip stuff. – km1 Aug 20 '12 at 04:08
  • Yes, it would definitely be easier, but what I'm trying to do is modify a file inside the jar. Then I found out you can't do that. So I got it working by extracting itself, putting a file inside it and compressing it again. I know I don't have to go through so much trouble, but I want it in only one `.jar` that you can copy wherever you want. –  Aug 20 '12 at 14:19
  • 2
    @Java - this is never going to be easy because it's so far removed from what jars were designed for - as an easy way to distribute application code and associated resources. I'd suggest any files your application should write to should be stored outside of the archive. Alternatively, if you're just trying to conditionally add resources to a jar, a build tool like Ant or Maven would help with that. – ryanp Aug 20 '12 at 19:56