简体   繁体   English

在源代码中和编译期间包含jar文件

[英]Include jar file in source code and during compile

I have made my custom jar file and compiled it using 我已经制作了自定义jar文件并使用它编译

path C:\Program Files\Java\jdk1.7.0_04\bin;%path%
javac *.java
jar cvf QLibrary.jar *.class

Then I took that jar file and put it in the same directory where my Main.java is located 然后我把那个jar文件放在我的Main.java所在的同一目录下

Main.java is going to use classes that are in the jar Main.java将使用jar中的类

So I have decided to put import QLibrary.*; 所以我决定把import QLibrary.*; inside Main.java Main.java

And I compile using 我编译使用

path C:\Program Files\Java\jdk1.7.0_04\bin;%path%
javac -cp ".;*.jar" *java

But apparently it does not recognize the library or the classes in it. 但显然它无法识别库或其中的类。 What exactly am I doing wrong? 我究竟做错了什么?

You should either list the names of all your jar files: 您应该列出所有jar文件的名称:

javac -cp ".;QLibrary.jar" *java

or you could use wildcards in this way: 或者你可以用这种方式使用通配符:

javac -cp ".;*" *java

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 . 类路径条目可以包含基本名称通配符* ,它被认为等同于指定扩展名为.jar.JAR的目录中所有文件的列表。 For example, the class path entry foo/* specifies all JAR files in the directory named foo. 例如,类路径条目foo/*指定名为foo的目录中的所有JAR文件。 A classpath entry consisting simply of * expands to a list of all the jar files in the current directory. 简单地由*组成的类路径条目扩展为当前目录中所有jar文件的列表。 ... For example, if the directory foo contains a.jar , b.jar , and c.jar , then the class path foo/* is expanded into foo/a.jar;foo/b.jar;foo/c.jar . ...例如,如果目录foo包含a.jarb.jarc.jar ,则类路径foo/*将扩展为foo/a.jar;foo/b.jar;foo/c.jar

http://docs.oracle.com/javase/6/docs/technotes/tools/windows/classpath.html http://docs.oracle.com/javase/6/docs/technotes/tools/windows/classpath.html

javac -cp选项中所述 ,如果您使用的是Windows ,请尝试使用java -cp .;QLibrary.jar mysource

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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