运行javac编译导入一些库的Java类时,如何加载jar文件?
下面是我的非常基本的示例Spring框架应用程序的结构运行javac编译导入一些库的Java类时,如何加载jar文件?,java,spring-mvc,jar,javac,Java,Spring Mvc,Jar,Javac,下面是我的非常基本的示例Spring框架应用程序的结构 libs/ com.springsource.org.apache.commons.logging-1.1.1.jar com.springsource.org.apache.log4j-1.2.15.jar jmxtools-1.2.1.jar org.springframework.asm-3.0.1.RELEASE-A.jar org.springframework.beans-3.0.1.RELEASE
libs/
com.springsource.org.apache.commons.logging-1.1.1.jar
com.springsource.org.apache.log4j-1.2.15.jar
jmxtools-1.2.1.jar
org.springframework.asm-3.0.1.RELEASE-A.jar
org.springframework.beans-3.0.1.RELEASE-A.jar
org.springframework.context-3.0.1.RELEASE-A.jar
org.springframework.core-3.0.1.RELEASE-A.jar
org.springframework.expression-3.0.1.RELEASE-A.jar
Test.java
下面是示例Java类Test.Java
,它导入了一些Spring框架库
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
public class Test {
public static void main(String[] args) {
Resource resource=new ClassPathResource("applicationContext.xml");
BeanFactory factory=new XmlBeanFactory(resource);
Student student=(Student)factory.getBean("studentbean");
student.displayInfo();
}
}
当我试图通过运行这个命令javac-d来编译这个Test
Java类时。Test.java
,我得到以下错误:
error: package org.springframework.beans.factory does not exist
import org.springframework.beans.factory.BeanFactory;
error: package org.springframework.beans.factory.xml does not exist
import org.springframework.beans.factory.xml.XmlBeanFactory;
error: package org.springframework.core.io does not exist
import org.springframework.core.io.ClassPathResource;
error: package org.springframework.core.io does not exist
import org.springframework.core.io.Resource;
error: invalid flag: libs/[EACH JAR FILE].jar
Usage: javac <options> <source files>
我想这是因为我没有在libs
目录中加载Spring框架jar文件。当我运行以下命令时,它解决了一个错误:
javac -cp libs/org.springframework.beans-3.0.1.RELEASE-A.jar -d . Test.java
如果我运行javac-cplibs/*.jar-d。Test.java
,我得到以下错误:
error: package org.springframework.beans.factory does not exist
import org.springframework.beans.factory.BeanFactory;
error: package org.springframework.beans.factory.xml does not exist
import org.springframework.beans.factory.xml.XmlBeanFactory;
error: package org.springframework.core.io does not exist
import org.springframework.core.io.ClassPathResource;
error: package org.springframework.core.io does not exist
import org.springframework.core.io.Resource;
error: invalid flag: libs/[EACH JAR FILE].jar
Usage: javac <options> <source files>
错误:无效标志:libs/[每个JAR文件].JAR
用法:javac
当试图用
javac
编译Java类时,如何加载(多个)jar文件?如果目录中有多个jar文件,并且希望编译一个Java类,可以按以下方式进行
javac -classpath lib/* <path to java file>/YourJavaFile.java
在这种情况下,不需要提及每个jar文件。实际上,提及
有不同的方式,并且它取决于操作系统。
对于Mac您必须在双引号“
javac-cp“libs/org.springframework.beans-3.0.1.RELEASE-A.jar”-d。
Test.java
或者您可以使用-classpath
javac-classpath“libs/org.springframework.beans-3.0.1.RELEASE-A.jar”
-d。Test.java
对于Ubuntu,您必须使用分号代码>
javac-cplibs/org.springframework.beans-3.0.1.RELEASE-A.jar-D
Test.java
相反,您可以在环境变量中添加所有依赖项jar文件,以避免每次都提到它的重复任务。尝试libs/*而不是“*.jar”