Randy Strauss
Randy Strauss

Reputation: 93

How do I properly add source files to a jar file, using the command line?

How do I add .java files to my jar? My classes, in the bin/ folder, are like: aaa/b/c/program.class and /aaa/b/c/d.class

So I add a/ to the jar and set the entry point to aaa.b.c.Program. Then I can: java -jar foo.jar and the program runs.

If I jar up the src/aaa/* and bin/aaa/* files, it isn't able to find aaa/b/c/Program.class - I tried several ways of specifying the entry point. It seem that the path must match the package+class name.

What's the proper way to make the jar file?

It won't let me add the java and class paths to the same folders easily. If I go to the parent folder and add the files:

To get them in the same folders, I can do it in two steps:

Is this the proper way?

If there are docs, do I put them in the same folders, too?

I searched a bunch on the web and am not finding this, even (especially?) in the lame Oracle docs...

(I tried to get Eclipse to do it- but it won't let me leave some classes out... But I really want to do this with the command line...)

Thanks

Upvotes: 1

Views: 600

Answers (1)

janos
janos

Reputation: 124734

If you want to have the compiled classes and the sources in the same folders, then you have to use two steps, a jar cf step to create the file with one set of files, and a jar uf step to add the other set of files, as in your example.

But doing this is not recommended. The recommended practice is to separate the runnable, the sources and the docs, and use a dedicated jar file for each. As you're doing something unusual, it's no surprise that there's no easy way to do it (you need two steps).

Upvotes: 1

Related Questions