I want to create a properties file on my classpath then put that file in my folder, but I do not understand: what is my classpath?
I know that classpath contains jars and references to the top level folders. I also read many related threads regarding the same (What is a classpath?) but I want some very basic explanation for it so that I won't be confused later.
My classpath variable contains multiple values so which is the exact path and where should I create my new file?
If I try to add new file to the project and file name is "jboss-ejb-client.properties", eclipse gives an error of invalid file name???
Thanks in advance.
A ClassPath is a path for a specific class:
for example you have a package called me.name.utils and you have a a class Bar in it.
The classpath to it will be: me.name.utils.Bar
Think about folders, you have a foo.exe file on your Desktop, in this case, the package will be: C:/Users/User/Desktop and the class will be foo.exe so the "classpath" will be C:/Users/User/Desktop/foo.exe
Except instead of files and folders, there are packages and classes (or interfaces/anything else)
Related
I came across this question:
What is a classpath and how do I set it?
and the first answer explainns what classpath is and how to set it:
.....First, let's suppose that MyClass is something you built as part of
your project, and it is in a directory in your project called output.
The .class file would be at
output/org/javaguy/coolframework/MyClass.class (along with every other
file in that package). In order to get to that file, your path would
simply need to contain the folder 'output', not the whole package
structure, since your import statement provides all that information
to the VM.
Now let's suppose that you bundle CoolFramework up into a .jar file,
and put that CoolFramework.jar into a lib directory in your project.
You would now need to put lib/CoolFramework.jar into your classpath.
The VM will look inside the jar file for the org/javaguy/coolframework
part, and find your class.
So, classpaths contain:
JAR files, and Paths to the top of package hierarchies....
but if java only looks for classes in directories specified by CLASSPATH variable how does java find classes from libraries that are part of JRE?
I am using web application in which jar files are there in lib folder. In web.xml, servlet class is provided.how does web.xml knows in which jar file that specific class is there???
This is handled by the classloader mechanism:
A JAR file usually contains a "manifest" -- a file which lists the
contents of the JAR. The manifest can define a JAR-class-path, which
further extends the class path (but only while loading classes from
that JAR). Classes accessed by a JAR-class-path are found in the
following order:
In general, classes referenced by a JAR-class-path entry are found as
though they were part of the JAR file. The JAR files that appear in
the JAR-class-path are searched after any earlier class path entries,
and before any entries that appear later in the class path. However,
if the JAR-class-path points to a JAR file that was already searched
(for example, an extension, or a JAR file that was listed earlier in
the class path) then that JAR file will not be searched again. (This
optimization improves efficiency and prevents circular searches.) Such
a JAR file is searched at the point that it appears, earlier in the
class path. If a JAR file is installed as an extension, then any
JAR-class-path it defines is ignored. All the classes required by an
extension are presumed to be part of the SDK or to have themselves
been installed as extensions.
source
The magic word in this case is class path .
The book "Learning in Java" describes this nicely.
https://www.safaribooksonline.com/library/view/learning-java/1565927184/ch03s03.html
Java CLASSPATH variable, holds a list of locations that can be searched for packages containing Java class files. The Java interpreter and Java compiler use CLASSPATH when searching for packages and classes on the local host.
Just like we add directory location to path variable in dos/linux we can specify the location of jars in classpath variable.
Java also provided some default locations that it will look in. For example the ext directory. The path of ext folder in java on a windows installation may look like "C:\Program Files\Java\jdk1.6.0\jre\lib\ext" .
You can put your jars in the ext folder and class files in them will be automatically located..
A location on the class path can be a directory name or the name of a class archive file. Java supports archives of class files in its own Java archive ( JAR) format . This allows large groups of classes to be distributed in a single file; the Java interpreter automatically extracts individual class files from an archive,when required.
The Java interpreter knows how to find core classes, which are the classes included in every Java installation. For example , the classes in the java.lang, java.io etc. Their location need not be put in class path, the Java interpreter can find them by itself.
To find other classes, the Java interpreter searches the locations on the class path in order.
For example consider a search for the class vehicle.cars.Porche. Searching the class path directory /usr/lib/java means the interpreter looks for an individual class file at /usr/lib/java/vehicle/cars/Porche.class. If the class is present in a jar file say CompanyVehicle.jar then the compiler will be looking for vehicle.cars.Porche.class in the CompanyVehicle.jar.
To sum it up , Java has a list of location it knows it should look into. Other than that, it will look into locations you provide in classpath variable.
if you want to understand more about how classpath, classloaders work behind the scene check out the links below
How JVM starts looking for classes?
http://javapapers.com/core-java/java-class-loader/
https://en.wikipedia.org/wiki/Classpath_(Java)
I have some classes in Eclipse which cannot be resolved to a type. I know that classes can be in .class, .jar, .par, .zip files. Are there any other file types that I have to look for? Or is there anything eclipse how I could make Eclipse recognize the classes?
In my general understanding, once I have found these files and added them to the classpath, the Eclipse should be able to recognize them.
Actually my colleague gave me an answer:
If the class cannot be found or resolved to a type within Eclipse, this is only the problem with build path. And to the build path, the files should be added of type as I have said: class, jar, par, zip; no other suffix is accepted.
Maybe I am misunderstanding your question, so please excuse me if I am. Type resolution errors are often due to incorrect imports or misspellings. Ill give an example:
public class Foo extends Component{
//If you forgot to import java.awt.*; You would receive the error
"Component cannot be resolved to a type"
}
Also if you misspelled an extension you would get the same error.When you create a class in eclipse you need to create a new file for that class.
You should also post your code so we know exactly what you're talking about. I don't think your error has anything to do with the classpath. :)
Class path entry could be jar, zip or directory (source)
set CLASSPATH=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 semi-colons. With the set command, it's important to omit spaces from around the equals sign (=).
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.
I have created a simple Java program (1 java file that contains the main() ), and I've included all Jar files in the same directory as the .class file. It is giving the NoClassDefFoundError message.
I've tried updating the Classpath to point to the directory, and I've also set "-cp ." to suggest that it look in the same directory as the .class file. However, the program still says it can't find the class def.
Any thoughts on what I should do?
Adding a folder tells java to look in that folder for .class files.
You can't reference .jar files via a folder name... Each .jar file's path must be listed on the CLASS_PATH explicitly.
this question's answer may be useful
When you try running a class from command line then a NoClassDefFound exception usualy means there is something wrong with your classpath.
You have explicitly define the classpath. You can do this in a few ways but the following way is the least prone to error:
Open a command shell and do the following:
1.) set classpath = {path to class files};{path to jars}
2.) java com.example.mainclass
Note: Even if your classes path and jar path is the same you need to specify them explicitly.
Note: If you have more then one jars place them in a folder say lib and add it to the classpath like: {path}/lib/* This will include all of the jar otherwise you have to specify them individually.
References: https://javarevisited.blogspot.com/2011/01/how-classpath-work-in-java.html
Import the following package:
Import java.lang.NoClassDefFoundError;
For test purpose I need to substitute .jar file with my own.
Does it possible set test.jar (the same name) previous on other test.jar?
Will classloader load only first find jar?
Thanks.
The Java interpreter will look for classes in the directories in the order they appear in the class path variable.
http://docs.oracle.com/javase/1.3/docs/tooldocs/win32/classpath.html
Edit: So you can put you jar first in the classpath to overrider the existing jar.