JAVA classpath jar problem [ZZ]

Source: Internet
Author: User

Classpath problem can be said to be all beginners headache problem, even the same.

1) The role of Classpath:
Its role ratione materiae let Java find the classes you want to perform, or that you own.

2) classpath settings:
Classpath can be set up in 2 places,

One is the system environment, such as Windows98 in the Autoexec.bat file, each boot automatically set.
Windows2000 system settings, add classpath items and so on.
The second is in the execution, such as Java-classpath c:\project;%classpath% MyClass
This adds the C:\Project directory to the original classpath.

3) use (How to use):
This may be the biggest problem, I said slowly.

Classpath is a bit like path, but not exactly.
Generally we declare the package name in a class:
Package Mypackage.first;
Import java.io.*;
public class Myfirst {...}

So it is to put myfirst this class into the mypackage in the first inside. You can think of MyPackage and first as directories, but you can't execute Java myfirst in Mypackage\first. Because Java will think that you are looking for the default directory (that is, classpath) to find the Myfirst class, rather than Mypackage.first inside the Myfirst class. What do you mean?
This is because Java will only look for Java in classpath, ignoring path. This is the only way to cross the platform.
Add one in Classpath. (dot) indicates the current directory, that is, the class that is looking for Java in the current directory, ignoring the other directories.
(Note: Classes in Java itself, such as Java.io.*;java awt.*, etc. are automatically included, not specified)

You may think this is troublesome, but this is very useful if you want to manage your files. For example, I have 10 EJB clients that correspond to different ejbs, so you can put the 10 clients into their own EJB packages to avoid confusion. Such as:
Myejb.helloworld.client;
Myejb.firstEJB.client; Wait a minute.

As an example:
Classpath=c:\project;d:\;
You have the First,helloworld directory in the C:\Project, which has its own client file (compiled).
Give an example of Client.java for the first directory:
Package first;
Import java.io.*;
public class Client
{
public static void Main (String args[])
{
System.out.println ("First.client invoked!!");
}
}
Compiling: Javac *.java in c:\project\first>
Then you can execute Java first.client in any DOS directory, which will start the client without starting HelloWorld, or anything else.

If you add it in the classpath. (points), such as classpath=.;D: \;
Then you have to execute the above statement in c:\project> because it means looking in d:\ and the current directory, otherwise Java will error classnofoundexception. Pay attention to this point.

I think this is almost the same, although a little short, but fine on the line, there is a problem keep abreast.

Thank you for reading this article.

Here, I give an example to make it easier for readers to understand the role of classpath.

Suppose there are 100 buildings, numbered from Building001 to Build100,
And each building has 100 households, also from Resident001 to Resident100 sequentially numbered.
These 100 buildings can be in Asia or in Europe.

So, the area here is the DOS directory (where you store the package).
The building here is a bit like the package.
The tenants here are the classes you create.
If you are a tenant in building 55th 39th, you can say:
Package Building55;
public class Resident39
{
...//Omit implementation code
}
Table of Contents:
C +
|------Asia (the role of the region can also serve as a package)
| |----Building001 (the role of the package)
|        | |-----Resident001 ... To Resident100 (A)
| |
| |----Building002
|        | |-----Resident001 ... To Resident100
| |
| |----Building003
|        | |-----Resident001 ... To Resident100
| |
| |----Building ...
|        | |-----resident .....
| |
| |----Building050
| |-----Resident001 ... To Resident100
|
|------Europe (the role of the region can also serve as a package)
|----Building051 (the role of the package)
|-----Resident001 ... To Resident100 (B)
|----Building052
|-----Resident001 ... To Resident100
|----Building053
|-----Resident001 ... To Resident100
|----Building ...
|-----resident .....
|----Building100
|-----Resident001 ... To Resident100

Here's a, there are a number of ways to express it: Recommended method (1)

1. Set classpath=c:\;

Java file: \ c \ asia \building001\resident001.java
Package Asia. Building001;
public class Resident001
{
...//omitted
}

Run: 1) \ c \ asia \building001>javac *.java
2) C: \ Any directory \>java Asia. building001.resident001

2. Set Classpath=c:\ Asia;

Java file: \ c \ asia \building001\resident001.java
Package building001;//is not "Asia." Building001 ".
public class Resident001
{
...//omitted
}
Run: 1) \ c \ asia \building001>javac *.java
2) c \ Any directory \>java building001.resident001
Note: If this is set, the package name conflict may be raised.
Such as:
Classpath=c:\ Asia; C: \ Europe;
Europe also has the same building001.resident001 words.

3. Set Classpath=c:\ Asia \building001;

Java file: \ c \ asia \building001\resident001.java

There is no package declaration statement.
public class Resident001
{
...//omitted
}
Run: 1) \ c \ asia \building001>javac *.java
2) c \ Any directory \>java building001.resident001
Note: This will have the same problem as (2), that is, the package name conflict.

Three methods have been introduced. Down there to make a problem.


is to access the package.
Actually, it's very simple. Just like visiting tenants.
Need to know the Intercontinental plate, building number (declaration of the package).
Need to go to which tenants, from which building go/or first to a certain section (hehe) ...
And not go directly to a tenant.
Such as:
C +
|------Asia (the role of the region can also serve as a package)
| |----Building001 (the role of the package)
|        | |-----Resident001 ... To Resident100 (A)
| |
| |----Building002
|        | |-----Resident001 ... To Resident100
....
(a) The expression is:
Package Asia. Building001;
public class Resident100
{
....//omitted
}
Well: In the case where the classpath is set correctly, you need to perform
C: \ Any directory \>java Asia. building001.resident100
Instead of
C # Asia \building001>java Resident100
Or
C # Asia >java building001.resident100

To summarize: Just the same level as the package declaration. cannot be omitted, nor can it be added.

JAVA classpath jar problem [ZZ]

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.