大部分的java程序应用于UNIX/Linux系统,而绝大部分的开发是在Windows下。虽然,java可以运行在anywhere, 但毕竟还有很多环境配置问题。
例如在UNIX下,你需要将某些配置文件的路径写入到另一个配置文件。 也许有很多局限,使你必须写入绝对路径。
在config.properties里写入
logs = /logs/app/db/logs.properties
configs=/usr/WebSphere/AppServer/installedApps/appname/earname/warname/WEB-INF/properties/myconfig.properties
在开发阶段,你是否愿意在你的Windows开发机上建立上面这样的目录,或者逐个修改这个路径呢? 尤其在已有的系统下,为了开发新的功能,构筑开发环境时,这种配置文件路径的修改是相当花时间的。 并且,在Release时,你必须要使用Ant工具批量修改这些配置文件。 但我可以说,大部分项目只有给生产和系统集成测试环境才会配置Ant工具。而在低级别的测试环境下,你只能手动更改。 那么如何才能不修改任何文件可以再windows本地调试并运行呢?
一下,我给出一个小小方案。
1. 重写java.io.File类!
先不要向我丢香蕉皮, 重写java.io.File并不一定要变动rt.jar文件。 jvm支持pretend,也就是伪装,我可以把我重写的java.io.File在运行期时代替rt.jar原有的java.io.File类。 想了解更详细的信息可以在 JAVA_HOME里找这个文件:[ JAVA_HOME]\bin\client\Xusage.txt
-Xbootclasspath/p:<directories and zip/jar files separated by ;>
prepend in front of bootstrap class path
在调试时,我就是要用这个参数。假设,我把重写的java.io.File类文件打包为filemap_1_4.jar。调试时,我就可以运行 java -Xbootclasspath/p:D:\MyProject\FileMap/filemap_1_4.jar -cp ...
这样,在我调用的所有类里,涉及到文件或文件系统功能时,都调用D:\MyProject\FileMap/filemap_1_4.jar 下面的java.io.File而不是rt.jar.
2. 功能实现
2.1 文件目录映射关系
为了增加一些灵活性, 我使用一个目录映射文件,来定义UNIX/LINUX文件路径和Windows文件路径的映射关系。
例如,filemap.properties
/usr/WebSphere/AppServer/installedApps/appname/earname/warname/=C:/MyProject/
/logs/app/db/=c:/MyProject/logs
当程序要读取/usr/WebSphere/AppServer/installedApps/appname/earname/warname/WEB-INF/properties/myconfig.properties
文件时,java.io.File会映射到C:/MyProject/WEB-INF/properties/myconfig.properties。
2.2 java.io.File更改
增加一个静态变量 private static HashMap filemaps=null;用来保存映射关系。
增加一个私有方法 initmaps初始化 filemaps
/**
* read filemap.propreties to initialize file map.
*/
private void initmaps(){
if(filemaps==null){
filemaps=new HashMap();
String filemap=System.getProperty("filemap"); //获得filemap.properties文件路径, 需要在jvm运行时传入-Dfilemap=[filemap.properties全路径名],不要试图使用 classloader.getResource(), 因为getResource里也会使用java.io.File,会产生jvm异常。
if(filemap==null || filemap=="")
return;
this.path = fs.normalize(filemap); //准备读取filemap.properties文件。因为使用FileInputStream时,需要传入一个java.io.File对象,在这暂 时把this.path设为filemap.properties的路径。
this.prefixLength = fs.prefixLength(this.path);
Properties pro=new Properties();
try {
pro.load(new FileInputStream(this)); //读取filemap.properties.
Enumeration enumeration=pro.propertyNames();
while(enumeration.hasMoreElements()){
String sourcepath=(String)enumeration.nextElement();
String targetpath=pro.getProperty(sourcepath);
filemaps.put(sourcepath, targetpath); //保存到filemaps静态对象里。
}
} catch(FileNotFoundException e1){
return;
} catch(IOException e2){
return;
}
}
}
我们还需要一个私有方法转换路径。
/**
* Get Virutal Path string
* @param name 原UNIX/Linux路径。
* @return 新windows路径。
*/
private String getVirtualPath(String name){
Iterator sources=filemaps.keySet().iterator();
while(sources.hasNext()){
String source=(String)sources.next();
if(name.startsWith(source)==true){ //当原路径包含filemaps里某一个source路径时,将原路径转换为新的target路径。
String target=(String)filemaps.get(source);
name=target+name.substring(source.length());
}
}
return name;
}
好了,现在准备在java.io.File里调用这两个方法
/**
* Creates a new <code>File</code> instance by converting the given
* pathname string into an abstract pathname. If the given string is
* the empty string, then the result is the empty abstract pathname.
*
* @param pathname A pathname string
* @throws NullPointerException
* If the <code>pathname</code> argument is <code>null</code>
*/
public File(String pathname) {
//new function
initmaps();
if (pathname == null) {
throw new NullPointerException();
}
//new function
pathname=getVirtualPath(pathname);
this.path = fs.normalize(pathname);
this.prefixLength = fs.prefixLength(this.path);
}
public File(String parent, String child) {
//new function
initmaps();
if (child == null) {
throw new NullPointerException();
}
//new function
child=getVirtualPath(child);
parent=getVirtualPath(parent);
if (parent != null) {
if (parent.equals("")) {
this.path = fs.resolve(fs.getDefaultParent(),
fs.normalize(child));
} else {
this.path = fs.resolve(fs.normalize(parent),
fs.normalize(child));
}
} else {
this.path = fs.normalize(child);
}
this.prefixLength = fs.prefixLength(this.path);
}
public File(File parent, String child) {
//new function
initmaps();
child=getVirtualPath(child);
if (child == null) {
throw new NullPointerException();
}
if (parent != null) {
if (parent.path.equals("")) {
this.path = fs.resolve(fs.getDefaultParent(),
fs.normalize(child));
} else {
String parentpath=getVirtualPath(parent.path);
this.path = fs.resolve(parent.path,
fs.normalize(child));
}
} else {
this.path = fs.normalize(child);
}
this.prefixLength = fs.prefixLength(this.path);
}
2.3 打包
将java.io.File编译并打包成jar文件。 filemap_1_4.jar
2.4 设置调试环境。
在你需要调试环境里不需要把这个jar文件发入classpath,但需要在VM arguments里加上
-Xbootclasspath/p:C:\MyProject\filemap_1_4.jar -Dfilemap=C:\MyProject\filemap.properties
3 测试
编写测试程序
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
FileReader filereader;
try {
//打印/usr/WebSphere/AppServer/InstallApp/Test.java
filereader = new FileReader(
"/usr/WebSphere/AppServer/InstallApp/Test.java");
BufferedReader bufferedreader = new BufferedReader(filereader);
String line=null;
while((line=bufferedreader.readLine())!=null)
System.out.println(line);
//遍历/usr/WebSphere/AppServer/InstallApp/Test.java所在的目录下所有文件,并打印文件名。
File fl=new File("/usr/WebSphere/AppServer/InstallApp/Test.java");
String path=fl.getParent();
String[] files=new File(path).list();
for(int i=0;i<files.length;i++){
System.out.println(files[i]);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
分享到:
相关推荐
这是《Understanding Unix/Linux Programming》书后光盘资源,这本书非常经典,是学习Unix/Linux的好教材,课后资源含有大量的练习。
Covering all the essential components of Unix/Linux, including process management, concurrent programming, timer and time service, file systems and network programming, this textbook emphasizes ...
2、熟悉UNIX/LINUX的常用基本命令如ls、who、w、pwd、ps、pstree、top等。 3、通过WINDOWS操作系统中的FTP程序,练习WINDOWS和UNIX之间的文件交换。 4、用vi编写一个简单的、显示"Hello,World!"的C程序,用gcc编译并...
操作系统接口:兼容Unix/Linux命令接口。 为Windows操作系统建立一个兼容Unix命令的命令接口;实现命令包括ls,cat,cp,mv,md/mkdir,rd/rmdir,cd,sort,more,print,命令的内容与详细格式请查阅unix命令手册;可以字符...
某银行的进程池管理代码,,Unix/linux进程池管理
unix/linux各种命令详解,非常详细
自己整理的unix/linux信号详解大全,很详细。
Unix/Linux 编程实践 附录光盘
《UNIX/Linux系统管理技术手册(第四版)》(ULAHv4)是《Linux系统管理技术手册》和《UNIX系统管理技术手册》的终结版,也是Evi Nemeth的封刀之作。 《UNIX/Linux系统管理技术手册(第四版)》延续了《Linux系统管理...
《Unix\Linux编程实践教程》通过解释Unix的工作原理,循序渐进地讲解实现Unix中系统命令的方法,让读者理解并逐步精通Unix系统编程,进而具有编制Unix应用程序的能力。书中采用启发式、举一反三、图示讲解等多种方法...
为Unix/Linux操作系统建立兼容的Windows/DOS命令接口,文件与目录命令; 具体命令:DIR(my_ls), RD(my_rd),CD,MD(my_md), DEL,MOVE(my_move),REN(my_ren),XCOPY, PROMPT,SORT(sort),TYPE(my_type),COPY(my_copy),...
Unix/Linux基础讲义.doc
unix-linux编程实践教程
Unix、Linux平台上的编程实践指南,以C/C++语言为编程语言。
介绍loadrunner监控 unix/linux所需要进程, 以及如何启动进程
Unix/linux系统的安全性概述 对于网站管理人员而言,日常性的服务器安全保护主要包括四方面内容: 文件存取合法性:任何黑客的入侵行为的手段和目的都可以认为是非法存取文件,这些文件包括重要数据信息、主页页面 ...
自购原版书和扫描仪,拆书制作,质量完美, 还未扫描完成,后续会发带目录的完整版本
近期UNIX/Linux漏洞.pdf
Unix/Linux命令详解