I'm a newbie when it comes to using Java on linux. I cloned a repository I'm interested in working on and I can't seem to run the code from my machine. The command I'm trying is:
java -Djava.library.path=/home/myname/path/to/project -jar example.jar
The error I'm getting is:
Error: Unable to access jarfile example.jar
First I thought it was a permissions thing, so I tried running the command with sudo (got the same error), I also saw one example with the filepath in quotes so I tried that, but I also got the same error.
I looked through the project files and there actually isn't a file named "example.jar". Is there a command I'm supposed to run first to generate it?
Thanks in advance for your help!
Yes, to run a jar, it has to exist.
While creating it, you have to match the directory structure to the package structure.
For instance, your directories might be:
for sources, and for class-files:
(often you find classes in a ../bin/.. instead of ../classes/.. named folder or source and class files are mixed in the same directories)
corresponding to a package structure:
then you should move to the base directory of the hierarchy:
or set it with the parameter -C
jar -C /home/ellen/proj/java/foo/example/classes ...
or from the homejar -C proj/java/foo/example/classes ...
as relative path.You create the jar file for example with:
and inspect it with:
Note that there is a manifest file, automatically generated and useful, as a template, where you can add the information of what your main class is, if you have such, but which is pretty strict about what the syntax is (upper/lower case, line breaks).
Such a main class entry has to be added, if you want to run it without specifying the main class:
or else you have to use:
In essence, a jar file is a zip archive and can be worked on with these tools.
A clean separation of src/ and bin/ files is useful, when you have more than a handful of classes and probably a deep packet structure.
will then, starting in the directory classes/ include everything which is often what you want. (example/class, example/util/.class, example/net/*class ...)
informs you about the syntax.