devoured elysium
devoured elysium

Reputation: 105227

Exporting Java project as .jar file will complain that certain .java files aren't found

When trying to export a project as a .jar file, Eclipse will complain that some files can't be found on the classpath:

Class files on classpath not found or not accessible for: 'proj/src/main/java/analysis/specification/VerifyVariableIsDefinedInsideTargetTerm.java'

Now, the interesting part is that these files haven't actually existed on the disk for ages. I guess I force Eclipse to refresh its database some way. I've tried doing F5 for a full build, but it seems to no avail. I've also tried the procedure described in this blog post, but the issue remains.

Upvotes: 11

Views: 12659

Answers (3)

devoured elysium
devoured elysium

Reputation: 105227

Error… I actually tripped on the reason for the problem a few moments ago…

All the files that were raising problems did actually exist on the project but were commented out (and thus, I didn't see them in the package explorer which is the view I generally use).

After deleting them, everything went fine. I still don't understand why would they raise errors in the JAR creation process, though…

EDIT: Here's an example of a commented out class:

package a.b.c

//import java.io.Closeable;

//public class Example implements Closeable {
//    public void close() throws IOException {
//        System.out.println("closed");
//    }
//}

Because the whole class is commented out, no Example.class is created. Whereas if the class is commented out like this:

package a.b.c

//import java.io.Closeable;

public class Example
//    implements Closeable
{
//    public void close() throws IOException {
//        System.out.println("closed");
//    }
}

an Example.class is created, although empty.

This may be a bug in eclipse. If you export the ant build script from eclipse and use that, it creates the JAR just fine.

Upvotes: 13

Aerophite
Aerophite

Reputation: 215

Probably the real problem is that not all your class files are in the same place… My suggestion is to move all your .java files into the same folder, recompile to make the .class files, make sure all the .class files are then in the same folder… Then, check out this webpage, if you need any help on actually creating the .jar.

The website also assumes you are on a Mac, but you should still be able to use a command prompt on Windows to get it work.

Upvotes: 0

Nirav Radia
Nirav Radia

Reputation: 177

This issue occurred to me also. However, the problem that I had was that I had JRE configuration issues in Eclipse.

Upvotes: 2

Related Questions