I am currently trying to write a program that reads the metadata of images using the library from here: https://github.com/drewnoakes/metadata-extractor/wiki/GettingStarted. My issue is that I cannot figure out how to compile the program using more than one .jar file (and it requires two). Both jar files are in my working directory, with the java file I'm trying to compile.
This is the command I am using, with just one .jar file referenced.
javac -cp metadata-extractor-2.7.2.jar MetadataPhotoExtractor.java
Thanks for any help
-Aaron
Use colon on unix-like systems as separator.
javac -cp metadata-extractor-2.7.2.jar:my-other-jar.jar MetadataPhotoExtractor.java
On Windows use semicolons because the colon is restricted for drive letter separation.
for windows
javac -cp metadata-extractor-2.7.2.jar;myother.jar MetadataPhotoExtractor.java
for Linux
javac -cp metadata-extractor-2.7.2.jar:myother.jar MetadataPhotoExtractor.java
More arguments are accountet as a list separated with : of the current parameter.
javac -cp lib1.jar:lib2.jar:lib3.jar myClass.java
Have you tried:
javac -cp metadata-extractor-2.7.2.jar:xmpcore-5.1.2.jar MetadataPhotoExtractor.java
Related
I have trying to compile java files at the windows command line using commands such as:
java myProg once I have used javac to create class files.
Problems arise when I use packages with a number of source files.
Often but not always I get main not found errors even though a main exists.
I am not quite sure what some of the directives mean and that is why it seems hit or miss.
Question
what does -cp mean exactly? java -cp src\myDirectory.myfile
sometimes I see:
./ infront of source eg .\src\myDirectory.myfile
on other sites I have found
% javac -cp .;stdlib.jar MyProgram.java
% java -cp .;stdlib.jar MyProgram
while compiling a jar library with java source files
what doesthe ".;" mean?
basically how do I compile three java source java files in one package at the windows command line and what does -cp and .; mean?
-cp means class path if I'm not mistaken.
try reading the following java docs
-classpath path
Specifies the path javac uses to look up classes needed to run javac or being referenced by other classes you are compiling. Overrides the default or the CLASSPATH environment variable if it is set. Directories are separated by semi-colons. It is often useful for the directory containing the source files to be on the class path. You should always include the system classes at the end of the path. For example:
javac -classpath .;C:\users\dac\classes;C:\tools\java\classes ...
https://www.cis.upenn.edu/~bcpierce/courses/629/jdkdocs/tooldocs/win32/javac.html
Answering your question directly, -cp means classpath or path.
Details on commandline arguments used while compiling and running a Java application can be found here: javac - Java programming language compiler
Extracting the description of -cp from that page:
-cp path or -classpath path:
Specify where to find user class files, and (optionally) annotation processors and source files. This class path overrides the user class path in the CLASSPATH environment variable. If neither CLASSPATH, -cp nor -classpath is specified, the user class path consists of the current directory. See Setting the Class Path for more details.
. means the current directory.
To compile multiple files in a directory use the following:
javac *.java // compliles all java files in the dir
java MyClass // runs the particular file
There are also a bunch of other related questions that should help you resolve this:
How to run a java program from the command line
How do I run java program with multiple classes from cmd?
Problems running a java program from the command line interface
Can't run multiple-class program from command line using packages
How can I add two or more jar file in the compile step using cmd?
using one jar file:
javac -g -cp YOUR_JAR.jar YOUR_FILE_NAME.java
Any ideas how to compile two jar files?
You can do that using the -classpath flag.
javac -classpath your.jar:my.jar ...
The delimiter between jars changes according to your platform.
You can read about that by running javac -help or reading the javac documentation online and About Setting the Class Path.
You will notice there that the documentation says:
Multiple path entries are separated by semicolons with no spaces
around the equals sign (=) in Windows and colons in Oracle Solaris.
So, all Xnix operating systems use a : as delimiter, whereas Windows use a ;.
As far as I understand, your question is about using multiple jars as the classpath when compiling java source code.
To use multiple jar/classpath files when compiling, you should separate them by your target platforms path separator, this is ';' on windows, and ':' on linux, example:
javac -g -cp FIRST.jar;SECOND.jar MY_FILE_NAME.java (windows)
javac -g -cp FIRST.jar:SECOND.jar MY_FILE_NAME.java (linux)
Sources: Including jars in classpath on commandline (javac or apt) (java and javac have the same classpath parsing
I have a Java code where I am importing Guava packages. I run it in windows command prompt using following commands:
javac -cp guava-11.0.2.jar Test.java
java -cp guava-11.0.2.jar;. Test
However, in Linux it is giving error. Can anybody help me to solve this issue.
The path separator on Linux/Unix is a colon, i.e. :.
So in your case the second command on Linux/Unix would be:
java -cp guava-11.0.2.jar:. Test
class path entries are separated by colons in Linux (not semicolons as in Windows)
Try that:
java -cp guava-11.0.2.jar:. Test
this is the command.
java -cp clojure.jar;sum.jar CalculateSum
sum.jar is a jar file made from clojure and java code.
CalculateSum is file which contains main method of java.
error from cygwin
can't execute binary file, Error 126
Cygwin provides you with a *nix environment within Windows, so that you might have to change the classpath separator to colons:
java -cp clojure.jar:sum.jar CalculateSum
Try:
java -cp clojure.jar:sum.jar:. CalculateSum
If you execute in the place you have the root of packages for CalculateSum.class
java -cp "clojure.jar;sum.jar" CalculateSum this is working
I have a folder on my desktop titled "Stuff" and in that folder I have the following:
Hello.java
mail.jar
And Hello.java imports from mail.jar, so I need to tell Hello.java to look for mail.jar.
From a Windows command line and from a unix command line, how can I compile this and run this?
Compile:
javac -cp .;mail.jar Hello.java
where ; is for Windows; use : for *nix.
and run:
java -cp .;mail.jar Hello
where again, use ; for Windows and : for *nix.
-cp tells both javac and java what classpath to use, and as your files are in the local directory where you're executing the command, you can use . for the Hello part and the name of the jar for the paths inside the jar. Wikipedia has a decent article on classpaths.
Mind you, if you're going to be doing this on a regular basis, you may want to set your CLASSPATH environment variable rather than constantly using the -cp flag. Both java and javac use the CLASSPATH variable.
For my own development machine, I actually include . in my CLASSPATH variable, for convenience. It's not something I would do on a production or build/test box, but it's very handy for development purposes. You'd want to have your usual jars in it as well.
Assuming Hello.java does not contain a package declaration, on Windows:
javac -cp mail.jar Hello.java
java -cp mail.jar;. Hello
The only difference on Unix platforms is that you separate the elements of the classpath with a scolon instead of a semicolon:
java -cp mail.jar:. Hello
Follow this tutorial and you should be able to do it in no time:
Java Compilation
You also shouldn't have any problems with the classpath because your classes are in the same folder