您的位置:首页 > 编程语言 > Java开发

java中如何获取文件路径

2015-04-08 13:34 316 查看
我们通常设置的环境变量中的CLASSPATH是 .;%JAVA_HOME%\lib\dt.jar;%JAVA_HOME%\lib\tools.jar,也就是当前目录加几个JAR包。此外,我们可以通过java中的-cp指定程序的classpath。但是,在程序中我们如何获得相应的文件呢?当然,我们可以使用绝对路径(这种方式让我们的程序无法移植),但是最有用的是使用getResource()和getResourceAsStream()来获得文件路径。下面来看一个简单的栗子:



(1)System.out.println(MainClass.class.getResource(""));

(2)System.out.println(MainClass.class.getResource("/"));

(3)System.out.println(MainClass.class.getClassLoader().getResource(""));

(4)System.out.println(MainClass.class.getResource("1.properties"));

(5)System.out.println(MainClass.class.getResource("/com/csii/parent/1.properties"));

(6)System.out.println(MainClass.class.getClassLoader().getResource("com/csii/parent/1.properties"));

(7)System.out.println(MainClass.class.getResource("/2.properties"));

(8)System.out.println(MainClass.class.getClassLoader().getResource("2.properties"));

这8条命令的输出如下:

ffile:/E:/J2EE_Workplace/GeneralTest/bin/com/csii/parent/

file:/E:/J2EE_Workplace/GeneralTest/bin/

file:/E:/J2EE_Workplace/GeneralTest/bin/

file:/E:/J2EE_Workplace/GeneralTest/bin/com/csii/parent/1.properties

file:/E:/J2EE_Workplace/GeneralTest/bin/com/csii/parent/1.properties

file:/E:/J2EE_Workplace/GeneralTest/bin/com/csii/parent/1.properties

file:/E:/J2EE_Workplace/GeneralTest/bin/2.properties

file:/E:/J2EE_Workplace/GeneralTest/bin/2.properties
也就是说Class.getResource("")获得的是当前类型所在目录

Class.getResource("/")获得的是classpath根目录(eclipse中是工程中的bin目录)

而Class.getClassLoader().getResource("")获得与Class.getResource("/")相同的目录。

这样,要获取1.properties和2.properties为什么要用以上命令也就一目了然啦~

对于jar包我们应该如何制定classpath呢?

You use either -jar or -cp, you can't combine the two. If you want to put additional JARs on the classpath then you should either put them in the main JAR's manifest and then use java -jar or you put the full classpath (including the main JAR and its dependencies)
in -cp and name the main class explicitly on the command line

java -cp 'MyProgram.jar:libs/*' main.Main

(I'm using the dir/* syntax that tells the java command to add all .jar files from a particular directory to the classpath. Note that the * must be protected from expansion by the shell, which is why I've used single quotes.)

You mention that you're using Ant so for the alternative manifest approach, you can use ant's <manifestclasspath> task after copying the dependencies but before building the JAR.

<manifestclasspath property="myprogram.manifest.classpath" jarfile="MyProgram.jar">

<classpath>

<fileset dir="libs" includes="*.jar" />

</classpath>

</manifestclasspath>

<jar destfile="MyProgram.jar" basedir="classes">

<manifest>

<attribute name="Main-Class" value="main.Main" />

<attribute name="Class-Path" value="${myprogram.manifest.classpath}" />

</manifest>

</jar>

With this in place, java -jar MyProgram.jar will work correctly, and will include all the libs JAR files on the classpath as well.
http://stackoverflow.com/questions/15930782/call-java-jar-myfile-jar-with-additional-classpath-option
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: