Why classpath=/tomcat_lib/ doesn't work - java

HI guys,
There is an abc.jar under /tomcat_lib. I need use this in my def.java
I tired
javac -classpath /tomcat_lib/ -d
../classes def.java
but it doesn't work
But if it works if I use
javac -classpath /tomcat_lib/abc.jar
-d ....
Can anyone help explain it?

To add a jar to your classpath, you need to specify the path up to and including the .jar file.
Quoting the official Java SE 6 documentation at Oracle.com:
Each [item in your classpath] should
end with a filename or directory
depending on what you are setting the
class path to:
For a .jar or .zip file
that contains .class files, the class
path ends with the name of the .zip or
.jar file.
For .class files in an
unnamed package, the class path ends
with the directory that contains the
.class files.
For .class files in a
named package, the class path ends
with the directory that contains the
"root" package (the first package in
the full package name).
...and from the "Folders and Archive Files" section of the same documentation:
When classes are stored in a directory
(folder), like
c:\java\MyClasses\utility\myapp, then
the class path entry points to the
directory that contains the first
element of the package name. (in this
case, C:\java\MyClasses, since the
package name is utility.myapp.)
But when classes are stored in an
archive file (a .zip or .jar file) the
class path entry is the path to and
including the .zip or .jar file.

Related

How do I get a JAR file in the current directory in the class path for a Java execution?

I have a class file Main.class which needs a JAR file abc.jar to run.
Both files are in the same directory. Now I try to run the class file with
java -cp "." Main
but I get a java.lang/NoClassDefFoundError.
I thought -cp "." tells the classpath to include the current directory, but somehow it doesn't.
How do I get this JAR file in the current directory on the class path?
Thanks to patrinox' comment I figured it out:
The JAR itself needs to be in the CLASSPATH property, not only the directory containing the JAR. Therefore the command line has to read:
java -cp ".:./abc.jar" Main

Will JVM only pick up .jar files?

When I give the -classpath argument to java to start will it pick up only files which have .jar extensions or will it attempt to look into other files in -classpath path as well?
Meaning if I specify -classpath to be /mypath which contains
/mypath/IAmANormalJar.jar
/mypath/IAmAJarWithoutExtension
where IAmAJarWithoutExtension is an actual jar file but without the .jar extension. Will only /mypath/IAmANormalJar.jar be loaded by JVM or will /mypath/IAmAJarWithoutExtension be loaded as well?
http://docs.oracle.com/javase/7/docs/technotes/tools/solaris/classpath.html
Classpath entries that are neither directories nor archives (.zip or
.jar files) nor * are ignored.
The wildcard symbol explained:
Class path entries can contain the basename wildcard character ,
which is considered equivalent to specifying a list of all the files
in the directory with the extension .jar or .JAR. For example, the
class path entry foo/ specifies all JAR files in the directory named
foo. A classpath entry consisting simply of * expands to a list of all
the jar files in the current directory. Files will be considered
regardless of whether or not they are hidden (that is, have names
beginning with '.').
Java classpath requires jar to be completed specified with extension. Without the extension Java treats that as a directory to which there are class files present to be added to the Java classpath. The Wikipedia article on the Java classpath provides some good information in this regard:
https://en.wikipedia.org/wiki/Classpath_(Java)
It will only pick up files with the extension .class, .jar or .zip. One trick I used to use when I wanted to flip between two jars for testing would be to add another extension to the file I wanted to be "out". If the version I wanted to test was mylib.jar, I would modify the old one as mylib.jar.old and they could co-exist in the directory.

What does java -cp "*" mean?

I was working on the Stanford sentiment classifier on windows. I wanted to retrain my own model, and here's how it was specified on the website:
java -mx8g edu.stanford.nlp.sentiment.SentimentTraining -numHid 25 -trainPath train.txt -devPath dev.txt -train -model model.ser.gz
But this gave me the error:
could not find or load main class
But on changing it to java -cp "*" it worked.
Class path entries can contain the basename wildcard character ,
which is considered equivalent to specifying a list of all the files
in the directory with the extension .jar or .JAR. For example, the
class path entry foo/ specifies all JAR files in the directory named
foo. A classpath entry consisting simply of * expands to a list of
all the jar files in the current directory.
From Oracle Docs
-cp < class search path of directories and zip/jar files>
Search all jar and zip files in the current directory for a given class file
The cp flag specifies the classpath, that is, which other archives are to be considered for this program.
Usually, you'd give it a colon-separated list of jar files, but this particular example is a special case according to the documentation:
If -classpath and -cp are not used and CLASSPATH is not set, then the user class path consists of the current directory (.). As a special convenience, a class path element that contains a base name of * is considered equivalent to specifying a list of all the files in the directory with the extension .jar or .JAR. A Java program cannot tell the difference between the two invocations.
Note that the quotes are necessary, beause otherwise the shell would exapand it to all the files in the directory rather than only jar files.

Classpath is not working

I got source code from Hermes (hermes2_src_20100121). When I extracted it has about 10 folders which is separate projects.
/myd1/Exetel/Hermes_SRC/ebxml-pkg
/myd1/Exetel/Hermes_SRC/CorvusEbMS
/myd1/Exetel/Hermes_SRC/Commons
Etc….
Each project has its own build script
/myd1/Exetel/Hermes_SRC/ebxml-pkg/ant/build.xml
/myd1/Exetel/Hermes_SRC/CorvusEbMS/ant/build.xml
/myd1/Exetel/Hermes_SRC/Commons/ant/build.xml
Etc….
I need to build this 10 projects using the given ant scripts
when I run it the script fails and it gives compilation error
/myd1/Exetel/Hermes_SRC/ebxml-pkg/src/hk/hku/cecid/ebms/pkg/PKISignatureImpl.java:98: error: package org.apache.log4j does not exist
etc.....
Issue in this, log4j can’t be found(similary dom4J, Mail, etc….)
So I created folder in path “/myd1/exete/libs/” and added the required jar file there and set the CLASSPATH to this folder
CLASSPATH=/myd1/exete/libs/*
but still it gives the same error as the classpath is not working
I tried add CLASSPATH to ~/.bashhrc
CLASSPATH=/myd1/exete/libs/*
Export CLASSPATH
Add CLASSPATH to gedit /etc/environment
Env | grep CLASSPATH prints the correct path
The issue is when I run build script it still gives the same error as the classpath is not working
You should either set classpath in ANT script:
<path id="common.classpath">
<fileset dir="/myd1/exete/libs">
<include name="*.jar"/>
</fileset>
</path>
or
Set CLASSPATH according to http://docs.oracle.com/javase/6/docs/technotes/tools/solaris/classpath.html:
classpath1:classpath2 Class paths to the .jar, .zip or .class files.
Each classpath should end with a filename or directory depending on
what you are setting the class path to: For a .jar or .zip file that
contains .class files, the class path ends with the name of the .zip
or .jar file. For .class files in an unnamed package, the class path
ends with the directory that contains the .class files. For .class
files in a named package, the class path ends with the directory that
contains the "root" package (the first package in the full package
name). Multiple path entries are separated by colons.
The default class path is the current directory. Setting the CLASSPATH
variable or using the -classpath command-line option overrides that
default, so if you want to include the current directory in the search
path, you must include "." in the new settings.
Classpath entries that are neither directories nor archives (.zip or
.jar files) nor * are ignored.
So you should try either:
CLASSPATH=/myd1/exete/libs/*
or
CLASSPATH=/myd1/exete/libs
Set your classpath as CLASSPATH=/myd1/exete/libs/myjar.jar
i.e. name your jar in the CLASSPATH!

Java: tips to add classpath

I have both a library.jar and program.jar in Java folder.
What is the correct command line to run? One method I tried is:
C:>java -cp c:\java\library.jar;.\java\program.jar program [param]
Try
java -cp c:\java\library.jar;.\java\program.jar package.the.MainClass [param]
From http://download.oracle.com/javase/1.3/docs/tooldocs/win32/classpath.html
Folders and archive files
When classes are stored in a directory
(folder), like
c:\java\MyClasses\utility\myapp, then
the class path entry points to the
directory that contains the first
element of the package name. (in this
case,C:\java\MyClasses, since the
package name is utility.myapp.)
But when classes are stored in an
archive file (a .zip or .jar file) the
class path entry is the path to and
including the .zip or .jar file. For
example, to use a class library that
is in a .jar file, the command would
look something like this:
C:> java -classpath C:\java\MyClasses\myclasses.jar utility.myapp.Cool
Multiple specifications
To find class files in the directory
C:\java\MyClasses as well as classes
in C:\java\OtherClasses, you would set
the class path to:
C:> java -classpath C:\java\MyClasses;C:\java\OtherClasses ...
Note that the two paths are separated
by a semicolon.
If you intend for your program.jar to be an executable JAR, you'll have to run it this way (and read this):
java -jar program.jar
Classpath entries can also contain the wildcard(*) character. For example, the class path entry C:\java\* specifies all JAR files in the C:\java directory and will be expanded into C:\java\library.jar;C:\java\program.jar.

Categories

Resources