Passing classes as args
I am attempting to pass 2 classes as args to a class called myAttempt but it is causing a fileNotFoundException.
I am using the following command-
java -classpath"%CLASSPATH%;C:\test;" myAttempt hW.class myObject.class
Does anyone have any idea how I can change this so it works correctly?
[340 byte] By [
colly10a] at [2007-10-2 6:24:21]

I depends what you're trying to do with those classes, and how you're tyring to do it.
If you want to actuall use those classes as regular Java classes--as opposed to just reading the class files as streams of bytes--then you'd just use hW and myObject (without the .class extensions). Of course, if they're in packages, then you'd need the fully qualified classnames.
What is your code trying to do with these classes?
> What is your code trying to do with these classes?
I just want to take in hW.class and myObject.class in as String arguement to the main method in myAttempt.
I have to pass hW and myObject as hW.class and myObject.class.
This will work if im in the directory which hold the classes-
java myAttempt hW.class myObject.class
but I cannot do that because im running this command from within another java program that I wrote so I need to use -classpath
Why do you want to take hW.class instead of hW? If you're going to use that class in your Java program, you need hW, not hW.class.
> Why do you want to take hW.class instead of hW? If
> you're going to use that class in your Java program,
> you need hW, not hW.class.
No I need to take them in as classes, I am using the class myAttempt to instrument the bytecode in the other 2 classes using the BCEL API, I am not running these classes within myAttempt, im just instrumenting them.
Okay, well, I don't know anything about that, and I don't know what its expectations are as far as its input classes/files.Why don't you post a bit of code along with the exact error you are getting?
If I try to guess what you are trying to do: you want to search for a file (hW.class) in all directories in your classpath?Here is one example (found by googling for "classpath split"): http://www.onjava.com/pub/a/onjava/excerpt/jebp_3/index1.html?page=3
> Okay, well, I don't know anything about that, and I
> don't know what its expectations are as far as its
> input classes/files.
>
> Why don't you post a bit of code along with the exact
> error you are getting?
The error is not coming from my program that works fine, it injects bytcode into the classes that I pass to it without problems providing I run it using -
java myAttempt hW.class myObject.class
But I am adding a UI to myAttempt and the UI will allow me to run the command above, but the command above will only work using the command above if myAttempt is in the same dir as the UI class.
That is why I want to know how to run the equivelent of the command above using something like-
java -classpath
rather that just java
When I use java -classpath I get this error
FileNotFoundException: hW.class (system cannot find the file specified)
hW.class is in the same dir as myAttempt so if the classpath finds one it should find the other.
Thanks for your help.
I hope that was a little clearer
Well, is it using the file system to find files or a ClassLoader? Sounds like the former, in which case your best bet is to provide full paths to the files, or use a JFileChooser to find the actual files, since you don't necessarily know what the pwd will be.
> Well, is it using the file system to find files or a
> ClassLoader? Sounds like the former, in which case
> your best bet is to provide full paths to the files,
> or use a JFileChooser to find the actual files, since
> you don't necessarily know what the pwd will be.
My program will specify the pwd and add it after -classpath. I will use a JFileChooser for the classes I want to pass to myAttempt and those classes are all in the same folder as myAttempt.
What I cannot understand is if all the files are in c:\temp and my pwd is c:\temp and I run using java myAttempt class1.class class2.class.......
then it works but if I am not in c:\temp and I use -classpath c:\temp it will not work.
Basically there seems to be no way to add args to main if I use -classpath
> What I cannot understand is if all the files are in
> c:\temp and my pwd is c:\temp and I run using java
> myAttempt class1.class class2.class.......
> then it works but if I am not in c:\temp and I use
> -classpath c:\temp it will not work.
Because apparently it uses File, FileInputStream, etc. rather than ClassLoader. Or if it's using a ClassLoader, the CL in question has pwd in its classpath but is not the System CL and hence doesn't use classpath.
I have no idea what BCEL or whatever is using to find the files you specify, but it looks like it's the file system, not a CL, which would explain why it behaves the way it does.
>
> When I use java -classpath I get this error
> FileNotFoundException: hW.class (system cannot
> find the file specified)
You are looking for a file.
Classpath impacts classes
The fact that your file has a class in it does not make it a class - you are still loading it as a file.
So the classpath will never have an impact.
There are various ways that you can load a file. One actually is relative to the classpath. But for that to work you have to retrieve the file as a resource (which I would guess you are not.)
Alternatively just provide the entire path to the file on the command line.