解决办法:Eclipse打包出现文件找不到的情况。

文章一出处:点击打开链接 http://tomfish88.iteye.com/blog/971255

文章二出处:点击打开链接 http://www.cnblogs.com/lanxuezaipiao/p/3291641.html

java获取当前类的绝对路径

在jsp和class文件中调用的相对路径不同。在jsp里,根目录是WebRoot 在class文件中,根目录是WebRoot/WEB-INF/classes 当然你也可以用System.getProperty("user.dir")获取你工程的绝对路径。

另:在Jsp,Servlet,Java中详细获得路径的方法!

1.jsp中取得路径:

以工程名为TEST为例:

(1)得到包含工程名的当前页面全路径:request.getRequestURI()
结果:/TEST/test.jsp
(2)得到工程名:request.getContextPath()
结果:/TEST
(3)得到当前页面所在目录下全名称:request.getServletPath()
结果:如果页面在jsp目录下 /TEST/jsp/test.jsp
(4)得到页面所在服务器的全路径:application.getRealPath("页面.jsp")
结果:D:\resin\webapps\TEST\test.jsp
(5)得到页面所在服务器的绝对路径:absPath=new java.io.File(application.getRealPath(request.getRequestURI())).getParent();
结果:D:\resin\webapps\TEST

2.在类中取得路径:

(1)类的绝对路径:Class.class.getClass().getResource("/").getPath()
结果:/D:/TEST/WebRoot/WEB-INF/classes/pack/
(2)得到工程的路径:System.getProperty("user.dir")
结果:D:\TEST

3.在Servlet中取得路径:

(1)得到工程目录:request.getSession().getServletContext().getRealPath("") 参数可具体到包名。
结果:E:\Tomcat\webapps\TEST
(2)得到IE地址栏地址:request.getRequestURL()
结果:http://localhost:8080/TEST/test
(3)得到相对地址:request.getRequestURI()
结果:/TEST/test

2011-01-04 11:40

另,Class类还有一个getResourceAsStream方法,记得以前有个项目要读取在同一个包内的一个xml,就用的这个。

1.如何获得当前文件路径
常用:
(1).Test.class.getResource("")
得到的是当前类FileTest.class文件的URI目录。不包括自己!
(2).Test.class.getResource("/")
得到的是当前的classpath的绝对URI路径
(3).Thread.currentThread().getContextClassLoader().getResource("")
得到的也是当前ClassPath的绝对URI路径
(4).Test.class.getClassLoader().getResource("")
得到的也是当前ClassPath的绝对URI路径
(5).ClassLoader.getSystemResource("")
得到的也是当前ClassPath的绝对URI路径
尽量不要使用相对于System.getProperty("user.dir")当前用户目录的相对路径,后面可以看出得出结果五花八门。
(6) new File("").getAbsolutePath()也可用。
       
2.Web服务器
(1).Tomcat
在类中输出System.getProperty("user.dir");显示的是%Tomcat_Home%/bin
(2).Resin
不是你的JSP放的相对路径,是JSP引擎执行这个JSP编译成SERVLET
路径为根.比如用新建文件法测试File f = new File("a.htm");
这个a.htm在resin的安装目录下 
(3).如何读文件
使用ServletContext.getResourceAsStream()就可以
(4).获得文件真实路径
String   file_real_path=ServletContext.getRealPath("mypath/filename");  
不建议使用request.getRealPath("/"); 
3.文件操作的类,不建议使用,可以使用commons io类

import java.io.*;
import java.net.*;
import java.util.*;


/**
* 此类中封装一些常用的文件操作。
* 所有方法都是静态方法,不需要生成此类的实例,
* 为避免生成此类的实例,构造方法被申明为private类型的。
* @since   0.1
*/

public class FileUtil {
   /**
    * 私有构造方法,防止类的实例化,因为工具类不需要实例化。
    */
   private FileUtil() {

   }

   /**
    * 修改文件的最后访问时间。
    * 如果文件不存在则创建该文件。
    * <b>目前这个方法的行为方式还不稳定,主要是方法有些信息输出,这些信息输出是否保留还在考

虑中。</b>
    * @param file 需要修改最后访问时间的文件。
    * @since   0.1
    */
   public static void touch(File file) {
     long currentTime = System.currentTimeMillis();
     if (!file.exists()) {
       System.err.println("file not found:" + file.getName());
       System.err.println("Create a new file:" + file.getName());
       try {
         if (file.createNewFile()) {
         //   System.out.println("Succeeded!");
         }
         else {
         //   System.err.println("Create file failed!");
         }
       }
       catch (IOException e) {
       //   System.err.println("Create file failed!");
         e.printStackTrace();
       }
     }
     boolean result = file.setLastModified(currentTime);
     if (!result) {
     //   System.err.println("touch failed: " + file.getName());
     }
   }

   /**
    * 修改文件的最后访问时间。
    * 如果文件不存在则创建该文件。
    * <b>目前这个方法的行为方式还不稳定,主要是方法有些信息输出,这些信息输出是否保留还在考

虑中。</b>
    * @param fileName 需要修改最后访问时间的文件的文件名。
    * @since   0.1
    */
   public static void touch(String fileName) {
     File file = new File(fileName);
     touch(file);
   }

   /**
    * 修改文件的最后访问时间。
    * 如果文件不存在则创建该文件。
    * <b>目前这个方法的行为方式还不稳定,主要是方法有些信息输出,这些信息输出是否保留还在考

虑中。</b>
    * @param files 需要修改最后访问时间的文件数组。
    * @since   0.1
    */
   public static void touch(File[] files) {
     for (int i = 0; i < files.length; i++) {
       touch(files);
     }
   }

   /**
    * 修改文件的最后访问时间。
    * 如果文件不存在则创建该文件。
    * <b>目前这个方法的行为方式还不稳定,主要是方法有些信息输出,这些信息输出是否保留还在考

虑中。</b>
    * @param fileNames 需要修改最后访问时间的文件名数组。
    * @since   0.1
    */
   public static void touch(String[] fileNames) {
     File[] files = new File[fileNames.length];
     for (int i = 0; i < fileNames.length; i++) {
       files = new File(fileNames);
     }
     touch(files);
   }

   /**
    * 判断指定的文件是否存在。
    * @param fileName 要判断的文件的文件名
    * @return 存在时返回true,否则返回false。
    * @since   0.1
    */
   public static boolean isFileExist(String fileName) {
     return new File(fileName).isFile();
   }

   /**
    * 创建指定的目录。
    * 如果指定的目录的父目录不存在则创建其目录书上所有需要的父目录。
    * <b>注意:可能会在返回false的时候创建部分父目录。</b>
    * @param file 要创建的目录
    * @return 完全创建成功时返回true,否则返回false。
    * @since   0.1
    */
   public static boolean makeDirectory(File file) {
     File parent = file.getParentFile();
     if (parent != null) {
       return parent.mkdirs();
     }
     return false;
   }

   /**
    * 创建指定的目录。
    * 如果指定的目录的父目录不存在则创建其目录书上所有需要的父目录。
    * <b>注意:可能会在返回false的时候创建部分父目录。</b>
    * @param fileName 要创建的目录的目录名
    * @return 完全创建成功时返回true,否则返回false。
    * @since   0.1
    */
   public static boolean makeDirectory(String fileName) {
     File file = new File(fileName);
     return makeDirectory(file);
   }

   /**
    * 清空指定目录中的文件。
    * 这个方法将尽可能删除所有的文件,但是只要有一个文件没有被删除都会返回false。
    * 另外这个方法不会迭代删除,即不会删除子目录及其内容。
    * @param directory 要清空的目录
    * @return 目录下的所有文件都被成功删除时返回true,否则返回false.
    * @since   0.1
    */
   public static boolean emptyDirectory(File directory) {
     boolean result = false;
     File[] entries = directory.listFiles();
     for (int i = 0; i < entries.length; i++) {
       if (!entries.delete()) {
         result = false;
       }
     }
     return true;
   }

   /**
    * 清空指定目录中的文件。
    * 这个方法将尽可能删除所有的文件,但是只要有一个文件没有被删除都会返回false。
    * 另外这个方法不会迭代删除,即不会删除子目录及其内容。
    * @param directoryName 要清空的目录的目录名
    * @return 目录下的所有文件都被成功删除时返回true,否则返回false。
    * @since   0.1
    */
   public static boolean emptyDirectory(String directoryName) {
     File dir = new File(directoryName);
     return emptyDirectory(dir);
   }

   /**
    * 删除指定目录及其中的所有内容。
    * @param dirName 要删除的目录的目录名
    * @return 删除成功时返回true,否则返回false。
    * @since   0.1
    */
   public static boolean deleteDirectory(String dirName) {
     return deleteDirectory(new File(dirName));
   }

   /**
    * 删除指定目录及其中的所有内容。
    * @param dir 要删除的目录
    * @return 删除成功时返回true,否则返回false。
    * @since   0.1
    */
   public static boolean deleteDirectory(File dir) {
     if ( (dir == null) || !dir.isDirectory()) {
       throw new IllegalArgumentException("Argument " + dir +
                                          " is not a directory. ");
     }

     File[] entries = dir.listFiles();
     int sz = entries.length;

     for (int i = 0; i < sz; i++) {
       if (entries.isDirectory()) {
         if (!deleteDirectory(entries)) {
           return false;
         }
       }
       else {
         if (!entries.delete()) {
           return false;
         }
       }
     }

     if (!dir.delete()) {
       return false;
     }
     return true;
   }


   /**
    * 返回文件的URL地址。
    * @param file 文件
    * @return 文件对应的的URL地址
    * @throws MalformedURLException
    * @since   0.4
    * @deprecated 在实现的时候没有注意到File类本身带一个toURL方法将文件路径转换为URL。
    *              请使用File.toURL方法。
    */
   public static URL getURL(File file) throws MalformedURLException {
     String fileURL = "file:/" + file.getAbsolutePath();
     URL url = new URL(fileURL);
     return url;
   }

   /**
    * 从文件路径得到文件名。
    * @param filePath 文件的路径,可以是相对路径也可以是绝对路径
    * @return 对应的文件名
    * @since   0.4
    */
   public static String getFileName(String filePath) {
     File file = new File(filePath);
     return file.getName();
   }

   /**
    * 从文件名得到文件绝对路径
    * @param fileName 文件名
    * @return 对应的文件路径
    * @since   0.4
    */
   public static String getFilePath(String fileName) {
     File file = new File(fileName);
     return file.getAbsolutePath();
   }

   /**
    * 将DOS/Windows格式的路径转换为UNIX/Linux格式的路径
    * 其实就是将路径中的"\"全部换为"/",因为在某些情况下我们转换为这种方式比较方便,
    * 某中程度上说"/"比"\"更适合作为路径分隔符,而且DOS/Windows也将它当作路径分隔符。
    * @param filePath 转换前的路径
    * @return 转换后的路径
    * @since   0.4
    */
   public static String toUNIXpath(String filePath) {
     return filePath.replace('\\', '/');
   }

   /**
    * 从文件名得到UNIX风格的文件绝对路径
    * @param fileName 文件名
    * @return 对应的UNIX风格的文件路径
    * @since   0.4
    * @see #toUNIXpath(String filePath) toUNIXpath
    */
   public static String getUNIXfilePath(String fileName) {
     File file = new File(fileName);
     return toUNIXpath(file.getAbsolutePath());
   }

   /**
    * 得到文件的类型。
    * 实际上就是得到文件名中最后一个“.”后面的部分。
    * @param fileName 文件名
    * @return 文件名中的类型部分
    * @since   0.5
    */
   public static String getTypePart(String fileName) {
     int point = fileName.lastIndexOf('.');
     int length = fileName.length();
     if (point == -1 || point == length - 1) {
       return "";
     }
     else {
       return fileName.substring(point + 1, length);
     }
   }

   /**
    * 得到文件的类型。
    * 实际上就是得到文件名中最后一个“.”后面的部分。
    * @param file 文件
    * @return 文件名中的类型部分
    * @since   0.5
    */
   public static String getFileType(File file) {
     return getTypePart(file.getName());
   }

   /**
    * 得到文件的名字部分。
    * 实际上就是路径中的最后一个路径分隔符后的部分。
    * @param fileName 文件名
    * @return 文件名中的名字部分
    * @since   0.5
    */
   public static String getNamePart(String fileName) {
     int point = getPathLsatIndex(fileName);
     int length = fileName.length();
     if (point == -1) {
       return fileName;
     }
     else if (point == length - 1) {
       int secondPoint = getPathLsatIndex(fileName, point - 1);
       if (secondPoint == -1) {
         if (length == 1) {
           return fileName;
         }
         else {
           return fileName.substring(0, point);
         }
       }
       else {
         return fileName.substring(secondPoint + 1, point);
       }
     }
     else {
       return fileName.substring(point + 1);
     }
   }

   /**
    * 得到文件名中的父路径部分。
    * 对两种路径分隔符都有效。
    * 不存在时返回""。
    * 如果文件名是以路径分隔符结尾的则不考虑该分隔符,例如"/path/"返回""。
    * @param fileName 文件名
    * @return 父路径,不存在或者已经是父目录时返回""
    * @since   0.5
    */
   public static String getPathPart(String fileName) {
     int point = getPathLsatIndex(fileName);
     int length = fileName.length();
     if (point == -1) {
       return "";
     }
     else if (point == length - 1) {
       int secondPoint = getPathLsatIndex(fileName, point - 1);
       if (secondPoint == -1) {
         return "";
       }
       else {
         return fileName.substring(0, secondPoint);
       }
     }
     else {
       return fileName.substring(0, point);
     }
   }

   /**
    * 得到路径分隔符在文件路径中首次出现的位置。
    * 对于DOS或者UNIX风格的分隔符都可以。
    * @param fileName 文件路径
    * @return 路径分隔符在路径中首次出现的位置,没有出现时返回-1。
    * @since   0.5
    */
   public static int getPathIndex(String fileName) {
     int point = fileName.indexOf('/');
     if (point == -1) {
       point = fileName.indexOf('\\');
     }
     return point;
   }

   /**
    * 得到路径分隔符在文件路径中指定位置后首次出现的位置。
    * 对于DOS或者UNIX风格的分隔符都可以。
    * @param fileName 文件路径
    * @param fromIndex 开始查找的位置
    * @return 路径分隔符在路径中指定位置后首次出现的位置,没有出现时返回-1。
    * @since   0.5
    */
   public static int getPathIndex(String fileName, int fromIndex) {
     int point = fileName.indexOf('/', fromIndex);
     if (point == -1) {
       point = fileName.indexOf('\\', fromIndex);
     }
     return point;
   }

   /**
    * 得到路径分隔符在文件路径中最后出现的位置。
    * 对于DOS或者UNIX风格的分隔符都可以。
    * @param fileName 文件路径
    * @return 路径分隔符在路径中最后出现的位置,没有出现时返回-1。
    * @since   0.5
    */
   public static int getPathLsatIndex(String fileName) {
     int point = fileName.lastIndexOf('/');
     if (point == -1) {
       point = fileName.lastIndexOf('\\');
     }
     return point;
   }

   /**
    * 得到路径分隔符在文件路径中指定位置前最后出现的位置。
    * 对于DOS或者UNIX风格的分隔符都可以。
    * @param fileName 文件路径
    * @param fromIndex 开始查找的位置
    * @return 路径分隔符在路径中指定位置前最后出现的位置,没有出现时返回-1。
    * @since   0.5
    */
   public static int getPathLsatIndex(String fileName, int fromIndex) {
     int point = fileName.lastIndexOf('/', fromIndex);
     if (point == -1) {
       point = fileName.lastIndexOf('\\', fromIndex);
     }
     return point;
   }

   /**
    * 将文件名中的类型部分去掉。
    * @param filename 文件名
    * @return 去掉类型部分的结果
    * @since   0.5
    */
   public static String trimType(String filename) {
     int index = filename.lastIndexOf(".");
     if (index != -1) {
       return filename.substring(0, index);
     }
     else {
       return filename;
     }
   }
   /**
    * 得到相对路径
    * 文件名不是目录名的子节点时返回文件名。
    * @param pathName 目录名
    * @param fileName 文件名
    * @return 得到文件名相对于目录名的相对路径,目录下不存在该文件时返回文件名
    * @since   0.5
    */
   public static String getSubpath(String pathName,String fileName) {
     int index = fileName.indexOf(pathName);
     if (index != -1) {
       return fileName.substring(index + pathName.length() + 1);
     }
     else {
       return fileName;
     }
   }

}
4.遗留问题

目前new FileInputStream()只会使用绝对路径,相对没用过,因为要相对于web服务器地址,比较麻烦

还不如写个配置文件来的快哪

5.按Java文件类型分类读取配置文件

配 置文件是应用系统中不可缺少的,可以增加程序的灵活性。java.util.Properties是从jdk1.2就有的类,一直到现在都支持load ()方法,jdk1.4以后save(output,string) ->store(output,string)。如果只是单纯的读,根本不存在烦恼的问题。web层可以通过 Thread.currentThread().getContextClassLoader().
getResourceAsStream("xx.properties") 获取;Application可以通过new FileInputStream("xx.properties");直接在classes一级获取。关键是有时我们需要通过web修改配置文件,我们不 能将路径写死了。经过测试觉得有以下心得:

1.servlet中读写。如果运用Struts 或者Servlet可以直接在初始化参数中配置,调用时根据servletcontext的getRealPath("/")获取真实路径,再根据 String file = this.servlet.getInitParameter("abc");获取相对的WEB-INF的相对路径
例:
InputStream input = Thread.currentThread().getContextClassLoader().
getResourceAsStream("abc.properties");
Properties prop = new Properties();
prop.load(input);
input.close();
OutputStream out = new FileOutputStream(path);
prop.setProperty("abc", “test");
prop.store(out, “–test–");
out.close();

2.直接在jsp中操作,通过jsp内置对象获取可操作的绝对地址。
例:
// jsp页面
String path = pageContext.getServletContext().getRealPath("/");
String realPath = path+"/WEB-INF/classes/abc.properties";

//java 程序
InputStream in = getClass().getClassLoader().getResourceAsStream("abc.properties"); // abc.properties放在webroot/WEB-INF/classes/目录下
prop.load(in);
in.close();

OutputStream out = new FileOutputStream(path); // path为通过页面传入的路径
prop.setProperty("abc", “abcccccc");
prop.store(out, “–test–");
out.close();

3.只通过Java程序操作资源文件
InputStream in = new FileInputStream("abc.properties"); // 放在classes同级

OutputStream out = new FileOutputStream("abc.properties");
=======================================

Eclipse将引用了第三方jar包的Java项目打包成jar文件的两种方法

方案一:用Eclipse自带的Export功能

步骤1:准备主清单文件 “MANIFEST.MF”,

由于是打包引用了第三方jar包的Java项目,故需要自定义配置文件MANIFEST.MF,在该项目下建立文件MANIFEST.MF,内容如下:

Manifest-Version: 1.0
Class-Path: lib/commons-codec.jar lib/commons-httpclient-3.1.jar lib/commons-logging-1.1.jar lib/log4j-1.2.16.jar lib/jackson-all-1.8.5.jar
Main-Class: main.KillCheatFans

第一行是MAINIFEST的版本,第二行Class-Path就指定了外来jar包的位置,第三行指定我们要执行的MAIN java文件。

这里要注意几点:

复制代码
1、Class-Path: 和Main-Class: 后边都有一个空格,必须加上,否则会打包失败,错误提示为:Invalid header field;

2、假设我们的项目打包后为KillCheatFans.jar,那么按照上面的定义,应该在 KillCheatFans.jar的同层目录下建立一个lib文件夹(即lib文件和打包的jar文件
在同一个目录下),并将相关的jar包放在里面。否则将会出现“Exception in thread "main" java.lang.NoClassDefFoundError”的错误;

3、Main-Class后面是类的全地址,比如你的主文件是KillCheatFans.java,文件里打包为package com.main; 那么这里就写com.main.KillCheatFans,
不要加.java后缀,主文件地址写错将会出现“找不到或无法加载主类”的错误;

4、写完Main-Class后一定要回车(即最后一行是空白行),让光标到下一行,这样你生成的jar包才能找到你的主class去运行,

否则将会出现“jar中没有主清单属性”的错误。
复制代码

 

步骤2:右击Java工程选择Export—>选择JAR file—>Next

 

步骤3:选择要打包的文件,不需要的文件不必打包,减小打包后的jar文件大小,并进行选项配置如下

这里有几个选项:

* Export generated class files and resources 表示只导出生成的.class文件和其他资源文件
* Export all output folders for checked projects 表示导出选中项目的所有文件夹
* Export java source file and resouces 表示导出的jar包中将包含你的源代码*.java,如果你不想泄漏源代码,那么就不要选这项了
* Export refactorings for checked projects 把一些重构的信息文件也包含进去

 

步骤4:选择我们在第一步中自定义的配置文件路径,这一步很重要,不能采用默认选项

 

这里解释一下配置项:

* Generate the manifest file:是系统帮我们自动生成MANIFEST.MF文件,如果你的项目没有引用其他class-path,那可以选择这一项。
* Use existing mainfest from workspace:这是可以选择我们自定义的.MF文件,格式如上所写,引用了第三方包时选用。
* Seal content:要封装整个jar或者指定的包packet。
* Main class:这里可以选择你的程序入口,将来打包出来的jar就是你这个入口类的执行结果。

 

最后Finish,即生成了我们要的jar文件。

 

运行该jar文件有两种方式:

1. 在命令行下运行命令java -jar 你的jar文件名称,比如我的执行如下:

如果在jar中有一些System.out.prinln语句(如上执行结果),运行后不想在控制台输出而是保存在文件中方便以后查看,可以用一下命令:
java -jar KillCheatFans.jar > log.txt (这时命令行窗口不会有任何输出)
输出信息会被打印到log.txt中,当然log.txt自动生成,并位于和KillCheatFans.jar一个目录中。

 

2. 新建一个批处理文件,如start.bat,内容为:java -jar KillCheatFans.jar,放在jar文件同一目录下即可,以后点击自动运行即可,更加方便。

 

方案二:安装Eclipse打包插件Fat Jar

     方案一对于含有较多第三方jar文件或含有第三方图片资源等就显得不合适,太繁琐。这时可以使用一个打包的插件—Fat Jar。

     Fat Jar Eclipse Plug-In是一个可以将Eclipse Java Project的所有资源打包进一个可执行jar文件的小工具,可以方便的完成各种打包任务,我们经常会来打jar包,但是eclipse自带的打包jar似乎不太够用,Fat Jar是eclipse的一个插件,特别是Fat Jar可以打成可执行Jar包,并且在图片等其他资源、引用外包方面使用起来更方便。


安装方法:

1. Eclipse在线更新方法
Help > Install New Software > Add,
name:Fat Jar
location:http://kurucz-grafika.de/fatjar

2. Eclipse插件手动安装方法
下载地址:http://downloads.sourceforge.net/fjep/net.sf.fjep.fatjar_0.0.27.zip?modtime=1195824818&big_mirror=0
将解压出的plugins中的文件复制到eclipse安装目录中的plugins目录下,然后重启eclipse即可。

使用方法:

步骤1:右击工程项目选择Buile Fat Jar

 

步骤2:配置jar文件存放目录,主Main文件等,如下图

 

步骤3:选择所要用到的第三方jar包

 

最后Finish,即生成了我们要的jar文件,十分方便。



  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值