[java] Java classpath.

Started by
5 comments, last by Antony52 18 years, 7 months ago
I have this problem: I am using an engine where a 3d model is being read from a resource path. In order to do that I have to set a classpath to the directory the model resides e.g. C:\file\model, and then add the following string to the reader /obj/box.obj. Of course it works. But, what I want is for the user to select any obj file and be capable of loading. I dont want this to be fixed. I have used JFileChooser where a new window pops up and the user select the file which in turn is stored in "File file = chooser.getSelectedFile();". Since the reader of the engine needs a string I use the method "file.getName();" to take the string name of the file. I also use the method "file.getPath();" to take the string of the path where the file resides. So now what I want is: a way everytime the user selects a file for the path to be added to my classpath thus allowing the reader to read the file. Is there a way to add a classpath in the program itself and not in the IDE properties?
Advertisement
Yes, there is. I'm in a hurry right now, but here goes some code:

    public static final URLClassLoader createClassLoader() throws Exception {                File dir = new File("directory");        String[] files = dir.list(new JARFilenameFilter()); // custom filter                URL[] urls = new URL[ files.length ];        for (int x = 0; x < files.length; x++) {            File file = new File("directory&#47;" + files[x]);            urls[x] = file.toURL();        }                return new URLClassLoader(urls);    }
a.k.a javabeats at yahoo.ca
Not sure if I'm reading this right, but..

The file doesn't *need* to be in your classpath for you to read it. The classpath is just the first place Java will look, if you it ask for a file without an absolute path.

But if you have a File object, that's all you need to get the contents of the file. Just create a FileInputStream or FileReader and start reading. If this engine requires the file as a string, give it the fully qualified name (File.getAbsolutePath), and it should be able to find it.
Since the obj reader method of the engine is doing all the reading then I dont need a FileInputStream right?
Pinacolada said it all, dude. Can you be a bit more specific on the issue? What engine are you using?
a.k.a javabeats at yahoo.ca
Quote:Original post by Antony52
Since the obj reader method of the engine is doing all the reading then I dont need a FileInputStream right?


Right
Yes you are right. AgentFX. I get the point of pinacolada just wanted to clarify I understand. Thanks guys.

This topic is closed to new replies.

Advertisement