0

I am trying to read a *.class resource from a package. I am using the following code to achieve this...

URL path = SomeClass.class.getResource("/source/someClass.class");

When I run this code, I end up with path = null;. However, if I use the same code and try to access other resources such as *.gif and *.txt files, it is successful. Is there a reason why I can't access the *.class files?

wattostudios
  • 8,666
  • 13
  • 43
  • 57
Anton Putov
  • 1,951
  • 8
  • 34
  • 62

2 Answers2

0

Re-read the javadoc. I think it describes the two ways you can use getResource():

http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/Class.html#getResource(java.lang.String)

Try the following:

URL path = Class.getResource( "modified/package/SomeClass" )

You might want to use:

InputStream stream = Class.getResourceAsStream("modified/package/SomeClass")

since you are only trying to copy class files into a Jar file.

chubbsondubs
  • 37,646
  • 24
  • 106
  • 138
  • I tryed next : URL url = Class.getResource(".package.SomeClass") - not work. – Anton Putov Apr 17 '12 at 06:56
  • You did it wrong. That's why it didn't work. Use Class.getResource("package/SomeClass"). The javadoc states you must replace the '.'in a fully qualified classname with '/'. – chubbsondubs Apr 17 '12 at 14:37
0

May be resource path is incorrect. Because the class directory is special to project.

You can try the following:

URL path = SomeClass.class.getResource("PackagePath/someClass.class");

the PackagePath like "/com/xxx/projectTest".

Mark Yao
  • 408
  • 2
  • 6