|
-
Jul 3rd, 2008, 09:22 AM
#1
Thread Starter
Frenzied Member
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?
-
Jul 3rd, 2008, 08:06 PM
#2
Re: creating instance of a class from another directory
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
-
Jul 4th, 2008, 04:02 AM
#3
Thread Starter
Frenzied Member
Re: creating instance of a class from another directory
i mean, is it possible without editing TestPlayer.java or ComputerPlayerInterface.java in any way
-
Jul 4th, 2008, 07:17 AM
#4
Thread Starter
Frenzied Member
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
-
Jul 4th, 2008, 07:27 AM
#5
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
-
Jul 5th, 2008, 04:55 AM
#6
Thread Starter
Frenzied Member
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.
-
Jul 5th, 2008, 05:01 AM
#7
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
-
Jul 5th, 2008, 05:40 AM
#8
Thread Starter
Frenzied Member
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?
-
Jul 5th, 2008, 06:00 AM
#9
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
-
Jul 6th, 2008, 05:23 PM
#10
Thread Starter
Frenzied Member
Re: creating instance of a class from another directory
 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.
-
Jul 7th, 2008, 12:28 AM
#11
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|