Most Java programs are used in Unix/Linux systems, while most of them are developed in windows. Although Java can run in anywhere, there are many environment configuration problems after all.
For example, in UNIX, you need to write some configuration file paths to another configuration file. There may be many limitations that make it necessary to write absolute paths.
Write in config. Properties
Logs =/logs/APP/DB/logs. Properties
Configs =/usr/WebSphere/appserver/installedapps/appname/earname/warname/WEB-INF/properties/myconfig. Properties
In the development phase, are you willing to create such a directory on your windows development machine, or modify the path one by one? Especially in existing systems, it takes quite some time to modify the path of the configuration file to develop new functions and build a development environment. In addition, you must use the ant tool to modify these configuration files in batches during release. However, I can say that ant tools can be configured only for production and system integration testing environments in most projects. In a low-level test environment, you can only manually change it. So how can I debug and run Windows locally without modifying any files?
I will provide a small solution.
1. Rewrite the java. Io. File class!
Don't drop me a banana peel first. Rewriting java. Io. file doesn't have to change the Rt. jar file. JVM supports pretend, that is, disguise. I can replace the java. Io. file I have rewritten with rt. Jar's original Java. Io. File class at runtime. For more details, refer to the java_home file: [java_home]/bin/client/xusage.txt.
-Xbootclasspath/P: <directories and zip/jar files separated by;>
Prepend in front of Bootstrap class path
This parameter is used during debugging. Suppose that I package the rewritten java. Io. File class file as filemap_ipv4.jar. During debugging, I can run Java-xbootclasspath/P: D:/myproject/filemap/filemap_ipv4.jar-CP...
In this way, all classes I call the java. Io. File under D:/myproject/filemap/filemap_1_4.jar instead of RT. jar when file or file system functions are involved.
2. function implementation
2.1 file directory ing
To increase flexibility, I used a directory ing file to define the ing between Unix/Linux File paths and Windows File paths.
For example, filemap. Properties
/Usr/WebSphere/appserver/installedapps/appname/earname/warname/= C:/myproject/
/Logs/APP/DB/= C:/myproject/logs
When the program wants to read/usr/WebSphere/appserver/installedapps/appname/earname/warname/WEB-INF/properties/myconfig. Properties
Java. Io. File maps to C:/myproject/WEB-INF/properties/myconfig. properties.
2.2 java. Io. File changes
Add a static variable Private Static hashmap filemaps = NULL to save the ing relationship.
Add a private method initmaps to initialize filemaps
/**
* Read filemap. propreties to initialize file map.
*/
Private void initmaps (){
If (filemaps = NULL ){
Filemaps = new hashmap ();
String filemap = system. getproperty ("filemap"); // obtain filemap. properties file path, which must be passed in-dfilemap = [filemap. properties Full path name], do not try to use classloader. getresource (), Because Java is also used in getresource. io. file.
If (filemap = NULL | filemap = "")
Return;
This. Path = FS. normalize (filemap); // prepare to read the filemap. properties file. When using fileinputstream, You need to input a java. Io. File object. At this time, set this. Path to the path of filemap. properties.
This. prefixlength = FS. prefixlength (this. Path );
Properties pro = new properties ();
Try {
Pro. Load (New fileinputstream (this); // read filemap. properties.
Enumeration enumeration = pro. propertynames ();
While (enumeration. hasmoreelements ()){
String sourcepath = (string) enumeration. nextelement ();
String targetpath = pro. getproperty (sourcepath );
Filemaps. Put (sourcepath, targetpath); // save it to the static object of filemaps.
}
} Catch (filenotfoundexception E1 ){
Return;
} Catch (ioexception E2 ){
Return;
}
}
}
We also need a private method to convert the path.
/**
* Get virutal path string
* @ Param name: original Unix/Linux path.
* @ Return the new windows path.
*/
Private string getvirtualpath (string name ){
Iterator sources = filemaps. keyset (). iterator ();
While (sources. hasnext ()){
String source = (string) sources. Next ();
If (name. startswith (source) = true) {// when the original path contains a Source Path in filemaps, convert the original path to the new target path.
String target = (string) filemaps. Get (source );
Name = target + name. substring (source. Length ());
}
}
Return name;
}
Now we are ready to call these two methods in 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. getdefaparparent (),
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. getdefaparparent (),
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 Packaging
Compile Java. Io. File and package it into a jar file. Filemap_ipv4.jar
2.4 set the debugging environment.
You do not need to send this jar file to classpath In the debugging environment, but you need to add it to the VM arguments
-Xbootclasspath/P: C:/myproject/filemap_1_4.jar-dfilemap = C:/myproject/filemap. Properties
3 Test
Compile a test program
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;
Try {
// Print/usr/WebSphere/appserver/installapp/test. Java
Filereader = new filereader (
"/Usr/WebSphere/appserver/installapp/test. Java ");
Bufferedreader = new bufferedreader (filereader );
String line = NULL;
While (line = bufferedreader. Readline ())! = NULL)
System. Out. println (line );
// Print the file name in the directory where/usr/WebSphere/appserver/installapp/test. Java is located.
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 ();
}
}
}