Glam Prestige Journal

Bright entertainment trends with youth appeal.

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!

1

1 Answer

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:

/home/ellen/proj/java/foo/example/src/example/Foo.java
/home/ellen/proj/java/foo/example/src/example/util/MyList.java 

for sources, and for class-files:

/home/ellen/proj/java/foo/example/classes/example/Foo.class
/home/ellen/proj/java/foo/example/classes/example/util/MyList.class

(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:

package example
package example.util

then you should move to the base directory of the hierarchy:

cd /home/ellen/proj/java/foo/example/classes

or set it with the parameter -C jar -C /home/ellen/proj/java/foo/example/classes ... or from the home jar -C proj/java/foo/example/classes ... as relative path.

You create the jar file for example with:

jar -cf example.jar *.class

and inspect it with:

jar -tf example.jar 

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).

Manifest-Version: 1.0
Created-By: 9-internal (Oracle Corporation)
Main-Class: example.Main

Such a main class entry has to be added, if you want to run it without specifying the main class:

java -jar example.jar 

or else you have to use:

java -cp example.jar example.Main

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.

 jar -cf example.jar -C classes . 

will then, starting in the directory classes/ include everything which is often what you want. (example/class, example/util/.class, example/net/*class ...)

 jar --help 

informs you about the syntax.

7

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy