java中如何获取文件路径
2015-04-08 13:34
316 查看
我们通常设置的环境变量中的CLASSPATH是 .;%JAVA_HOME%\lib\dt.jar;%JAVA_HOME%\lib\tools.jar,也就是当前目录加几个JAR包。此外,我们可以通过java中的-cp指定程序的classpath。但是,在程序中我们如何获得相应的文件呢?当然,我们可以使用绝对路径(这种方式让我们的程序无法移植),但是最有用的是使用getResource()和getResourceAsStream()来获得文件路径。下面来看一个简单的栗子:
![](http://img.blog.csdn.net/20150430002846656)
(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
(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
相关文章推荐
- 关于web工程中java类如何获取webapp路径下的文件
- 关于web工程中java类如何获取webapp路径下的文件
- java中如何获取当前文件的物理路径?
- java如何获取jsp页面上传的文件路径 20 前台jsp: <form action="AA.do?method=yktAdd" method="post" > <input type='text'
- 关于web工程中java类如何获取webapp路径下的文件
- 如何获取当前程序文件的路径 Current Path
- 如何获取当前程序文件的路径 Current Path
- 如何获取当前程序文件的路径
- 怎样在JAVA文件中获取该项目的相对路径
- 如何获取当前程序文件的路径 Current Path
- 如何获取当前程序文件的路径
- 如何获取共享文件的路径
- v在JAVA文件中获取该项目的相对路径
- java 如何获取当前程序的路径
- 在JAVA文件中获取该项目的相对路径
- 【转载】如何获取当前程序文件的路径
- JAVA 程序中如何拷贝一个目录下的文件及子目录到另一个目录,如何获取系统环境变量等...
- 相对路径获取JAVA配置文件Class.getResourceAsStream() & ClassLoader的getResourceAsStream()
- LyNews『凌云新闻』 - 在JAVA文件中获取该项目的相对路径
- 转帖--在JAVA文件中获取该项目的相对路径