I write an application that load other classes from other folder like so :
public static void main(String[] args) throws Exception
{
String rootPath = null;
rootPath = System.getProperty("user.dir");
//System.out.println(rootPath);
File operatorFile = new File(rootPath + "Business\\");
URL operatorFilePath = operatorFile.toURL();
URL[] operatorFilePaths = new URL[]{operatorFilePath};
ClassLoader cl = new URLClassLoader(operatorFilePaths);
// Plus , Minus,Multiply,Divide are classes in Business folder
Class[] operatorClass = new Class[]{ cl.loadClass("Plus"), cl.loadClass("Minus"),cl.loadClass("Multiply") , cl.loadClass("Divide")
};
I Want Dynamically get the Business folder path(My main class is in MyFolder). Both MyFolder and Business folders are in drive D:\ . But this app dose not work because I think below statement dose not correct :
File operatorFile = new File(rootPath + "Business\\");
Can anyone help me?
Since you Have to Remeber Some Points .
System.getProperty("user.home");
//Either C or D whatever .or :
rootPath = System.getProperty("user.dir")+System.getProperty("file.separator")+"Business";
or
String rootPath = System.getProperty("user.dir")+System.getProperty("file.separator")+"D"+System.getProperty("file.separator")+"MyFolder";
rootPath=rootPath.replaceAll("MyFolder", "");//Remove MyFolder from URL
rootPath=rootPath+"Business";//Add Business on URL
System.out.println(""+rootPath);//Print File Path
that will Return your Home Directory With Business Folder
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments