Results 1 to 11 of 11

Thread: creating instance of a class from another directory

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Sep 2005
    Posts
    1,364

    creating instance of a class from another directory

    Say for e.g.

    My java application lies in C:\myProject

    I have
    D:\stuff\TestPlayer.java
    D:\stuff\ComputerPlayerInterface.java

    now from my application dir, how could i load an instance of TestPlayer?

    Code:
    	    Class c = Class.forName("TestPlayer");
    
    	    ComputerPlayerInterface o = (ComputerPlayerInterface) c.newInstance();
    but the above code means that TestPlayer has to be in C:\myProject but i want to get this working to create an instance of TestPlayer with it being in a different folder

    Class c = Class.forName("D:\stuff\TestPlayer") does not work.

    any ideas guys, is this even possible, without editing TestPlayer.java or ComputerPlayerInterface.java?

  2. #2
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: creating instance of a class from another directory

    is this even possible
    A different folder means a different package.. and for no specific reason I think you need to read a little about Java
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Sep 2005
    Posts
    1,364

    Re: creating instance of a class from another directory

    i mean, is it possible without editing TestPlayer.java or ComputerPlayerInterface.java in any way

  4. #4

    Thread Starter
    Frenzied Member
    Join Date
    Sep 2005
    Posts
    1,364

    Re: creating instance of a class from another directory

    ok heres how my project works.

    I have a tomcat server which my application runs on (in the form of an applet)

    my project is in this directory:
    C:\Program Files\Apache Software Foundation\Tomcat 6.0\webapps\eg\ACS

    (the applet and necessary java files are in that folder)

    Now my system is about an automated competition. so students can submit their own java files (their own computer players) online.

    Now when a students submits their file, all submissions go into a folder like this:

    C:\Program Files\Apache Software Foundation\Tomcat 6.0\webapps\eg\submissions\ms98

    (where ms98 is the students registered username - this folder is automatically created if it doesnt already exist)

    So for e.g, if ms98 submits TestPlayer.java then the path for it wil be

    C:\Program Files\Apache Software Foundation\Tomcat 6.0\webapps\eg\submissions\ms98\TestPlayer.java

    as i said above, my applet stuff is in:
    C:\Program Files\Apache Software Foundation\Tomcat 6.0\webapps\eg\ACS

    im wanting to dynamically load and run TestPlayer.java which is not in the same directory.

    i hope you guys can help me

  5. #5
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: creating instance of a class from another directory

    1st of all, If you're doing an applet, there is no need to mention tomcat you can use any server with plain HTML.
    About your problem: you can't do that unless they both belong to the same package. If so you can just move the file to your complied classes path
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

  6. #6

    Thread Starter
    Frenzied Member
    Join Date
    Sep 2005
    Posts
    1,364

    Re: creating instance of a class from another directory

    Thanks for the reply ComputerJy. I tried this stuff with packages too now. But when i run the applet, it keeps giving me the classnotfound exception. im doing the necessay imports and classpaths etc.

    In C:\Program Files\Apache Software Foundation\Tomcat 6.0\webapps\eg\ACS I have AppletTest.java
    Code:
    import java.io.*;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.JOptionPane;
    import java.applet.*;
    import submissions.*;
    
    public class AppletTest extends JApplet {
        public void init() {
    	Container contentPane = getContentPane();
    	contentPane.setBackground(Color.white);
    	contentPane.setLayout(new BorderLayout());
    
    	try {
    	    Class c = Class.forName("TestPlayer");
    	    ComputerPlayerInterface object = (ComputerPlayerInterface) c.newInstance();
    	}
    
    	catch (Exception e) { msgbox(e.toString()); }
        }
    
    
    
        public void msgbox(String message) {
    	JOptionPane.showMessageDialog(null, message);
        }
        
    }
    In C:\Program Files\Apache Software Foundation\Tomcat 6.0\webapps\eg\ACS\submissions I have

    TestPlayer.java
    Code:
    package submissions;
    
    public class TestPlayer implements ComputerPlayerInterface {
    
        public String test() {
    	return "Tom";
        }
    }
    ComputrPlayerInterface.java
    Code:
    package submissions;
    
    public interface ComputerPlayerInterface {
        
        // Method signatures, which all submitted computer players must have.
        String test();
    }
    but still when i run the applet, it gives me the msgbox saying classnot found

    My classpath environment variable from advanced setting of 'My Computer' is
    %SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem;C:\Program Files\Java\jdk1.5.0_05\bin;C:\Program Files\Microsoft SQL Server\90\Tools\binn\;C:\Program Files\Apache Software Foundation\Tomcat 6.0\webapps\eg\ACS\submissions\
    any idea whats going wrong?
    Last edited by Pouncer; Jul 5th, 2008 at 05:04 AM.

  7. #7
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: creating instance of a class from another directory

    You can't add classes to the classpath at runtime. you can only relaunch
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

  8. #8

    Thread Starter
    Frenzied Member
    Join Date
    Sep 2005
    Posts
    1,364

    Re: creating instance of a class from another directory

    so there's nothing i can do then?

    apart from have all files in the same directory?

  9. #9
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: creating instance of a class from another directory

    no, nothing you need to do. This should be enough
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

  10. #10

    Thread Starter
    Frenzied Member
    Join Date
    Sep 2005
    Posts
    1,364

    Re: creating instance of a class from another directory

    Quote Originally Posted by ComputerJy
    no, nothing you need to do. This should be enough
    I was able to solve the problem by adding the .class files into a jar file, then placing that jar file in the project directory (programatically)

    and then putting the .jar file in the archive tag of the applet.

  11. #11
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: creating instance of a class from another directory

    My solution wouldn't have worked for applets of course (my bad). But the solution you came up with is brilliant my friend
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width