as above, isit possible?
Welcome to the Java Programming Forums
The professional, friendly Java community. 21,500 members and growing!
The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.
>> REGISTER NOW TO START POSTING
Members have full access to the forums. Advertisements are removed for registered users.
as above, isit possible?
You'll have to be more specific.. what do you mean by "placing packages of the same directory"?
src packages
hmm... so if I understand you correctly, you want to create an array of File references to all files in the current directory?
If this is the case, I'd recommend using the listFiles() method (you'll need to check if the file is indeed a file and not a directory, and if you want, recurse down the folder structure)
File directory = new File("./someDirectory"); File[] allFiles = directory.listFiles();
the problem is, say i cant import all the packages in src.
example:
import src.*;
get what i mean?
if i can import them then putting them in an array wouldnt be a problem :'(!!!
... so you want to dynamically link .class files with your program? In which case, a quick google search came back with this:
Java reflection
javanub:( (May 12th, 2010)
yeah man, thats something i wanna do like what i posted in my previous thread. trying to read on it. im a nub hehe
thanks helloworld
yeah, reflection is totally what i need. using strings to get classes and its methods. wooo
Just out of interest, what are you actually trying to achieve?
running other classes in a class automatically
final Class<?> clazz = Class.forName("my.class.package.and.name.Here"); final Object object = clazz.newInstance();
Now just cast it to whatever it is you know it will be or use more reflection on the object to run methods.
// Json
javanub:( (May 17th, 2010)
thanks was able to do it. here's my code
ClassLoader classLoader = RunExistingBuildAndTestplan.class .getClassLoader(); try{ Class aClass = classLoader.loadClass("testscripts." + caseName + "." + caseName); System.out.println("aClass.getName() = " + aClass.getName()); Method mainMethod = aClass.getMethod("main", new Class[] { String[].class }); mainMethod.invoke(null, new Object[] { new String[] { "", "" } }); }
Grats, nice to see you solved your problem.
// Json
If you're running the main(..) method, wouldn't it be simpler to use Runtime.exec(..) to execute the other application, or do you have to share the same JVM ?
yup, sharing the same jvm. does it allow multiple classes run at the same time?