简体   繁体   中英

How do I set up and use classes, libraries, packages in Java?

I'm new enough to the idea of using external class libraries and APIs not native to Java that I don't even know if I'm asking the right questions with the right terminology. So let me explain what I'm doing.

I need to read and write to an Excel file. I found this page that has a Java-Excel API. I want to get that API installed and available for my local instance of Java ( C:\\java\\ ) so I can just import these classes when i create a new program (eg, import jxl.CellView ). I have a zip file from sourceforge , but after that, I'm just lost.

The instructions reference setting it up in Eclipse; I'm not a fan of Eclipse as an IDE...so I want to know how to do it "manually". I think this will help me better understand why it works the way it does for the future.

So, my next instruction is... "To use this library in your Java program add the lib jxl.jar to your classpath in your project. See Changing classpath in Eclipse." Do I just unzip the zip file to C:\\java ? Is that what they mean by my class path? What are the steps I need to follow from here to ensure I can import the appropriate classes as needed?

Further, do these classes get bundled with applets? When I compile a java program, will the recipient of that file be able to run it even if they don't have this particular package installed on the machine on which they're running it?

Can someone give me some perspective on this?

I like to put all my third party libraries in c:\\java\\lib. That way I can keep them separate from the JREs and JDKs I have in c:\\java. This is not strictly necessary, but it's nice to be tidy. Here are some instructions specific to using the library you're interested in.

Extract jexcelapi_2_6_12.zip to c:\\java\\lib\\ .

This should mean the following file now exists: C:\\java\\lib\\jexcelapi\\jxl.jar .

Let's put your code in c:\\java\\work for now, to keep it separate. Create the file c:\\java\\work\\MyClass.java using a text editor:

import jxl.CellView;
public class MyClass {
    public static void main(String[] args) {
        System.out.println("I can access " + CellView.class);
    }
}

Open a command prompt and change directory to c:\\java\\work.

cd /dc:\\java\\work

Now compile the class:

javac -classpath ..\\lib\\jexcelapi\\jxl.jar MyClass.java

If you get no errors, the directory should now contain a new file MyClass.class , which is your compiled program.

If you see the error: 'javac' is not recognized as an internal or external command, operable program or batch file. then you could specify the full path to javac.exe. The following example assumes you have a JDK located in c:\\java\\jdk1.6.0_37, which may or may not be true on your system:

c:\\java\\jdk1.6.0_37\\bin\\javac -classpath ..\\lib\\jexcelapi\\jxl.jar MyClass.java

Run your compiled program:

C:\\java\\work>java -classpath ..\\lib\\jexcelapi\\jxl.jar;. MyClass

Output:
I can access class jxl.CellView

Notice the semicolon and dot at the end of the java classpath. This adds the current directory to the classpath so that java can find MyClass.class. That was not necessary with javac .

Unlike PATH , which should contain only directories, java's CLASSPATH (or -classpath ) can contain both files and directories. When a file appears in the classpath it should be a JAR. Since JARs are just zip files, you sometimes also see ZIP files in classpaths. This is non-standard, but it works. Java will treat the .zip file as a .jar. If you needed to add some second library to your program, separate the files with a semicolon (Windows) or colon (*nix).

javac -classpath ..\\lib\\jexcelapi\\jxl.jar;..\\lib\\lib-2.0\\lib-2.0.jar MyClass.java

Sometimes libraries consist of more than one jar file. In this case, you will need to add every JAR file that is a part of the library to the classpath. Again, separate them with semicolons. If this seems tedious, you'll start to see why people like to use IDEs or Ant to work with Java rather than command line commands.

There are many things still to learn like using packages, using the environment variable CLASSPATH to set the classpath instead of using the -classpath command line argument, adding your JDK bin directory to your PATH, adding more than one entry to the classpath, creating shell scripts to make rebuilding and rerunning your program easier, using IDEs for development, and Maven/Ant for building, making your own JARs, ... the list of things to learn goes on and on. This post is not meant to indicate best practices, but rather to get you up and running quickly. Follow the Oracle documentation and other best practices posted on the Internet to keep improving your Java development environment.

Further, do these classes get bundled with applets?
If your program is eventually going to be an applet, then yes, you will need to distribute jxl.jar with your program -- people are not going to have it on their computer, nor are they going to want to download it on their own. I am not going to describe how to make Applets here. There are lots of resources on the web. Google for it and come back with more specific questions about problems you run into.

When I compile a java program, will the recipient of that file be able to run it even if they don't have this particular package installed on the machine on which they're running it?
The jxl library does not get compiled into your program. You will need to deliver the jxl library to your users somehow, along with your program. You should probably bundle your program up into a JAR, since most non-trivial programs consist of more than one Java class, and that means your program will consist of multiple .class files. There are a number of ways to approach this problem. Spend some time reading resources on how to create applets and then come back to stackoverflow with more specific questions.

Go to the command line.

Type in:

echo %CLASSPATH%

It should return a list of directories. You can put the library you want to import into any of them.

Read: http://docs.oracle.com/javase/tutorial/essential/environment/paths.html

The classpath is a list of directories and .jar files, from which the the Java virtual machine loads classes it needs. Some directories and jars are in there by default (basically the libraries that come with the Java installation), others have to be specified by the user.

One way to specify additional places it the CLASSPATH environment variable. If there is more than one entry separate it by the platform's path separator (semicolon on Windows, colon for most other systems).

The other way is the -classpath option when you start the compiler or the java virtual machine.

In your case, put the jxl.jar file somewhere (say /some/path/jxl.jar ) and then invoke the compiler as (assuming, MyClass is in the default package and stored in the current directory)

javac -classpath /some/path/jxl.jar:. MyClass.java

and to run it use

java -classpath /some/path/jxl.jar:. MyClass

in the zip file you have jxl.jar, you have to include this jxl.jar in your proyect like external library. For example in netbeans:

http://jsumon.wordpress.com/2009/11/24/adding-external-jar-or-library-to-netbeans-java-project/

and eclipse:

http://www.wikihow.com/Add-JARs-to-Project-Build-Paths-in-Eclipse-%28Java%29

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM