0

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
  • Cloned a Repository? Like from Github? And when you "Build" the Project it creates a Jar file that you can Run? I'm confused with your wording. Please edit your question with a link to the repository. Apr 19, 2018 at 8:21

1 Answer 1

1

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
  • @karel Thank you! This explanation is very detailed and helps a lot. There actually aren't any .class files either, the structure is basically just a src folder with a list of java files. From googling a bit more, I think I need to compile them to create the class files and then possibly create a manifest file as well - then go on to create the .jar and run the project.
    – ellen
    Apr 19, 2018 at 19:19
  • @karel I'm trying to compile using javac -cp /path *.java and it doesn't seem to be able to find the package I'm trying to include. I posted about my new problem here: stackoverflow.com/questions/49927382/…
    – ellen
    Apr 19, 2018 at 19:21
  • @ellen: That's right, you have to create the class files by compiling them from the source, before you can pack them into a jar. And for compiling them, there is a similar dependency between directories and package structure as well. In the -cp-part, you have to include third party libraries as well as the base dir of your project, like -cp /opt/ibm/ILOG/CPLEX_Studio_Community128/cplex/lib/cplex.jar:. (the /opt/ibm....cplex part seems doubled in your linked posting and another pitfall is the case sensitivity of identifiers - generously handled by windows but not by java. Apr 19, 2018 at 20:14
  • So the convention for java packages is: everything in lowercase, like "ilog.concert" which should then be in a directory ".../ilog/concert/" and the parent of "ilog" being the base dir. But I guess the "javac" java compiler is tolerant for uppercase dirs on Windows and the "java" runtime should be too. However, you have to inspect the jar file, whether directories match the naming of the package names, in there, and it is easier, if directory names match by default. Apr 19, 2018 at 20:20
  • @karel thanks! As far as I can tell there is no case issue. I'm on ubuntu 14.04 if it helps. I tried adding the : . to the path but no dice. I'm not sure what you mean by "doubled in your linked posting", can you clarify?
    – ellen
    Apr 20, 2018 at 1:44

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .