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.

Results 1 to 17 of 17

Thread: placing packages of the same directory into an array

  1. #1
    Member
    Join Date
    Jan 2010
    Posts
    42
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default placing packages of the same directory into an array

    as above, isit possible?


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: placing packages of the same directory into an array

    You'll have to be more specific.. what do you mean by "placing packages of the same directory"?

  3. #3
    Member
    Join Date
    Jan 2010
    Posts
    42
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: placing packages of the same directory into an array

    src packages

  4. #4
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: placing packages of the same directory into an array

    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();

  5. #5
    Member
    Join Date
    Jan 2010
    Posts
    42
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: placing packages of the same directory into an array

    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 :'(!!!

  6. #6
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: placing packages of the same directory into an array

    ... so you want to dynamically link .class files with your program? In which case, a quick google search came back with this:
    Java reflection

  7. The Following User Says Thank You to helloworld922 For This Useful Post:

    javanub:( (May 12th, 2010)

  8. #7
    Member
    Join Date
    Jan 2010
    Posts
    42
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: placing packages of the same directory into an array

    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

  9. #8
    Member
    Join Date
    Jan 2010
    Posts
    42
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: placing packages of the same directory into an array

    yeah, reflection is totally what i need. using strings to get classes and its methods. wooo

  10. #9
    Forum old-timer
    Join Date
    Nov 2008
    Location
    Faversham, Kent, UK
    Posts
    472
    My Mood
    Mellow
    Thanks
    4
    Thanked 58 Times in 54 Posts

    Default Re: placing packages of the same directory into an array

    Just out of interest, what are you actually trying to achieve?

  11. #10
    Member
    Join Date
    Jan 2010
    Posts
    42
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: placing packages of the same directory into an array

    running other classes in a class automatically

  12. #11
    Forum old-timer
    Join Date
    Nov 2008
    Location
    Faversham, Kent, UK
    Posts
    472
    My Mood
    Mellow
    Thanks
    4
    Thanked 58 Times in 54 Posts

    Default Re: placing packages of the same directory into an array

    Quote Originally Posted by javanub:( View Post
    running other classes in a class automatically
    Can you explain what you mean by that, or give an example situation?

  13. #12
    Member
    Join Date
    Jan 2010
    Posts
    42
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: placing packages of the same directory into an array

    Quote Originally Posted by dlorde View Post
    Can you explain what you mean by that, or give an example situation?
    using the string i retrieve from the server, i am able to link that string to those classes using reflection

  14. #13
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: placing packages of the same directory into an array

    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

  15. The Following User Says Thank You to Json For This Useful Post:

    javanub:( (May 17th, 2010)

  16. #14
    Member
    Join Date
    Jan 2010
    Posts
    42
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: placing packages of the same directory into an array

    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[] { "", "" } });
    }

  17. #15
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: placing packages of the same directory into an array

    Grats, nice to see you solved your problem.

    // Json

  18. #16
    Forum old-timer
    Join Date
    Nov 2008
    Location
    Faversham, Kent, UK
    Posts
    472
    My Mood
    Mellow
    Thanks
    4
    Thanked 58 Times in 54 Posts

    Default Re: placing packages of the same directory into an array

    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 ?

  19. #17
    Member
    Join Date
    Jan 2010
    Posts
    42
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: placing packages of the same directory into an array

    yup, sharing the same jvm. does it allow multiple classes run at the same time?

Similar Threads

  1. About META-INF directory?
    By chinni in forum Java Servlet
    Replies: 2
    Last Post: February 9th, 2010, 10:36 AM
  2. where can i find the tmp/foo directory?
    By Idy in forum Java IDEs
    Replies: 11
    Last Post: January 19th, 2010, 11:21 AM
  3. A Question Regarding Abstract Classes & Packages
    By Kerubu in forum Object Oriented Programming
    Replies: 3
    Last Post: December 21st, 2009, 01:27 PM
  4. importing packages..
    By chronoz13 in forum Java IDEs
    Replies: 4
    Last Post: November 23rd, 2009, 05:49 PM
  5. Class directory
    By Kit in forum Java Theory & Questions
    Replies: 7
    Last Post: October 11th, 2009, 10:07 AM