且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

javac编译文件和jar,但是java失败

更新时间:2023-09-15 23:24:46

您使用设置了classpath的类来编译您的类:

You compile your class with the classpath set :

javac -classpath lib/*jar *.java 

但是您不会在设置了classpath的情况下启动可运行类:

but you don't launch the runnable class with the classpath set :

java TheFrame

此外,在.和扩展名.jar. "rel =" nofollow noreferrer>设置类路径文档以设置带有通配符的类路径.

Besides, the . and the extension .jar is not specified in the Setting the class path documentation to set a classpath with a wildcard.

类路径条目可以包含基本名称通配符, 这被认为等效于指定所有文件的列表 在扩展名为.jar或.JAR的目录中.例如, 类路径条目foo/指定目录中的所有JAR文件 foo. 仅由*组成的类路径条目扩展为所有列表 当前目录中的jar文件.

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. For example, the class path entry foo/ specifies all JAR files in the directory named foo. A classpath entry consisting simply of * expands to a list of all the jar files in the current directory.

包含*的类路径条目将与类文件不匹配.到 在单个目录foo中匹配类和JAR文件,请使用 foo; foo/*或foo/*; foo.选择的顺序确定是否 在foo中的JAR文件之前先加载foo中的类和资源,或者 反之亦然.

A class path entry that contains * will not match class files. To match both classes and JAR files in a single directory foo, use either foo;foo/* or foo/*;foo. The order chosen determines whether the classes and resources in foo are loaded before JAR files in foo, or vice versa.

子目录不是递归搜索的.例如,foo/*看起来 仅适用于foo中的JAR文件,而不能用于foo/bar,foo/baz等中.

Subdirectories are not searched recursively. For example, foo/* looks for JAR files only in foo, not in foo/bar, foo/baz, etc.

如果您的所有jar都位于lib文件夹的根目录下,这应该可以解决您的问题:

This should solve your problem if all your jar are located at the root of the lib folder:

java -classpath .:lib/*  TheFrame  

要编译您的类,应使用相同的synthax设置类路径.
令人惊讶的是,执行时没有编译错误:

To compile your class, you should use the same synthax to set the classpath.
It is surprising that you have no compilation error when you execute :

 javac -classpath lib/*jar *.java 

我尝试过:

javac -cp D:\repo\commons-lang3\3.1\*jar MyClass.java

我收到javac错误:

javac:无效标志: D:\ repo \ commons-lang3 \ 3.1 \ commons-lang3-3.1-sources.jar

javac: invalid flag: D:\repo\commons-lang3\3.1\commons-lang3-3.1-sources.jar

使用

javac -cp D:\repo\commons-lang3\3.1\* MyClass.java

编译很好.

java命令的行为完全相同.

I have exactly the same behavior with the java command.