My question is this is there a way to put all the Java Imports in a jar file, so when you make a Java Program no need to import the files ind开发者_JS百科ividually????
EDIT: I mean like
import java.awt.*;
import java.awt.event.*;
into a jar file where u don't need to type out the import java.awt.*;
everytime
You can use netbeans to do that http://java.sun.com/developer/technicalArticles/java_warehouse/single_jar/
If you're thinking of something like a C/C++ global include file that allows a single import statement at the top of files -- then no.
However, your Java IDE should be capable of adding imports for you automatically.
You are mixing two things - Java packages and jar files.
A package is not much more than a name space for classes often used together. The import java.awt.*
statement does not add the AWT classes to your program, it just lets you write Graphics
instead of java.awt.Graphics
and similar. There is nothing you can do to avoid importing them, if you don't want to spell them out (which is even more annoying).
To be able to use a class at runtime, this class has to be available to the virtual machine - independently whether you imported it or wrote its name fully. The standard classes like java.awt.*
are usually already known to your VM (since they are part of the runtime environment). To use other classes (including your own ones), you need to include them to the class path (e.g. by the -cp
argument to the java
command, or by putting them in the current directory), or you need to load them by an own classloader. Here you can use jar files - the class files can either be in jar files mentioned in the class path, or be inside of directories mentioned in the class path. (The URLClassloader works the same way.)
But again, packaging classes in jars does not avoid importing them to be able to use them. One of them concerns the compiler's name resolution, the other concerns the runtime class loading.
精彩评论