why i cannot doing this...so confusing..

i'm really stuck in here, i don't understand why this is always get an exception message.

Can someone help me? why i cannot doing this?

publicclass reflect

{

publicstaticvoid main(String[] args)

{

try

{

Class runClass = Class.forName("C:\\Eclipse\\Workspace\\Working\\Computer\\Hardware\\Internal\\Intens");

Field field = runClass.getDeclaredField("name");

System.out.println(field.toString());

}

catch(Exception e)

{

e.printStackTrace();

}

}

}

why i always got this message :

"java.lang.ClassNotFoundException:C:\Eclipse\Workspace\Skripsi\Working\Computer\Hardware\Internal\Intens"

i already compile Intens.java and result has no error, but why i cannot reflect the file...

thanks a lot..

[1319 byte] By [2Puluh11Lapan3a] at [2007-10-3 4:05:10]
# 1

Let's say the class is declared as

package working.computer.hardare.internal; // note packages are usually all lowercase

class Intens {}

Then you need to have C:\Eclipse\Workspace on your classpath, and you'd do Class.forName("working.computer.hardare.internal.Intens")

You don't specify the full path. You specify the fully qualified classname, including the whole package name, separated by dots. That package's root directory must be inside one of your classpath directories.

jverda at 2007-7-14 22:04:25 > top of Java-index,Core,Core APIs...
# 2

hey, thanks for the answer, it works...but is there any different way to doing something like this, but we don't have to set the classpath?

this is the structured :

"C:\Eclipse\Workspace\Working\ " is my working directory, which all project file included on it..

"Computer\" is my project folder, contains package named hardware and inside package hardware there is one java file, named Intens.java

this is inside Intens.java :

package hardware;

class Intens{}

basicly i change the structure from :

C:\Eclipse\Workspace\Working\Computer\Hardware\Internal\Intens

to

C:\Eclipse\Workspace\Working\Computer\Hardware\Intens

how to load the class without setting the classpath first, is that possible to do?

thanks a lot..

2Puluh11Lapan3a at 2007-7-14 22:04:25 > top of Java-index,Core,Core APIs...
# 3

In general, you have to specify the classpath. With Sun JVMs starting with 1.3 or 1.4, if there no explicitly specified classpath--either by the classpath environment variable or by the -cp or -classpath command line arg--then the classpath is assumed to be exactly the current directory.

For more info, google for java classpath tutorial and java package tutorial. You should find lots of info and examples. If you still have specific questions, please post them.

If you're using an IDE like eclipse you can set that project's classpath through the IDE's preferences, but that's only a solution for when you're working in that IDE.

jverda at 2007-7-14 22:04:25 > top of Java-index,Core,Core APIs...
# 4

Hey, thanks a lot, i already search it, it really helps...now i can work with package.

can i ask another question? i'm very sure you can help me, these are my questions :

>> If you're using an IDE like eclipse you can set that project's classpath through the IDE's preferences, but that's only a solution for when you're working in that IDE.

1) is there anyway to set classpath from inside java code? cos i try something like this but it gave me error message

Process proc = Runtime.getRuntime().exec("set CLASSPATH=C:/Eclipse/Workspace/Working/Computer");

2) the reason why i'm doing number (1) cos i want to make my own IDE like an Eclipse or any IDE, but i don't see any IDE using " set CLASSPATH " to use every files in current project , so i'm thinking maybe IDE load or read every Class files using the way like i do, but seems that i'm wrong, how can IDE implement this?

3) assume i have 2 files, first i put in C:/Eclipse/Workspace/Working/reflect.java and C:/Eclipse/Workspace/Working/Computer/hardware/Intens.java

=== reflect.java ===

//No package declaration

public class reflect

{

public static void main(String[] args)

{

try

{

//i'm trying to reflect C:/Eclipse/Workspace/Working/Computer/hardware/Intens

Process proc = Runtime.getRuntime().exec("javac -classpath C:/Eclipse/Workspace/Working Computer/hardware/Intens.java");

Class runClass = Class.forName("hardware.Intens");

Field field = runClass.getDeclaredField("name");

System.out.println(field.toString());

}

catch(Exception e)

{

e.printStackTrace();

}

}

}

=== Intens.java ===

package hardware;

//Just using one package declarations "hardware" i put these file in C:/Eclipse/Workspace/Working/Computer/hardware/Intens.java

public class Intens

{

public static String name = "ThisisJava";

public static void main(String[] args)

{

System.out.println("Java");

}

}

How to run reflect.java, cos it always gave me ( ClassNotFoundException ) when i try to run? if i declared package working.computer.hardware in Intens.java there is no problems of course i change the code in reflect.java but these is not what i want..

thank u very much...i hope u want to help and share your amazing knowledge..thanks again...

2Puluh11Lapan3a at 2007-7-14 22:04:25 > top of Java-index,Core,Core APIs...
# 5

> 1) is there anyway to set classpath from inside java

> code?

Yes and no.

You can't change the classpath and then just refer to classes on the new classpath the "normal" way. You can create a new ClassLoader (I usually just instantiate a new URLClassLoader, but you might want to write your own) and load classes using that one. This is not terribly hard, but there are some subtleties you have to be aware of. Try googling for java classloader tutorial or example.

> 2) the reason why i'm doing number (1) cos i want to

> make my own IDE like an Eclipse or any IDE, but i

> don't see any IDE using " set CLASSPATH " to use

> every files in current project , so i'm thinking

> maybe IDE load or read every Class files using the

> way like i do, but seems that i'm wrong, how can IDE

> implement this?

See above.

> How to run reflect.java, cos it always gave me (

> ClassNotFoundException )

cd to Working java -cp . reflect

or from anywhere do java -cp C:/Eclipse/Workspace/Working reflect

(Note that the convention is for package names to be all lowercase and class names to start with uppercase and use camel case within.)

Now, if you mean you're getting the error at the point where you do Class.forName, then it's because either the exec("javac") failed or you still don't have your classpath set up correctly. Since Intens is in the hardware package, and that package starts in the Computer directory, your classpath would have to also include C:/Eclipse/Workspace/Working/Computer

jverda at 2007-7-14 22:04:25 > top of Java-index,Core,Core APIs...
# 6

>> cd to Working

>> java -cp . reflect

>> or from anywhere do

>> java -cp C:/Eclipse/Workspace/Working reflect

Hi, why i still got the exception..this is step that i do : (type in DOS)

=== reflect.java ===

public class reflect

{

public static void main(String[] args)

{

try

{

Process proc = Runtime.getRuntime().exec("javac -classpath C:/Eclipse/Workspace/Working Computer/hardware/Intens.java");

Class runClass = Class.forName("hardware.Intens");

Field field = runClass.getDeclaredField("name");

System.out.println(field.toString());

}

catch(Exception e)

{

e.printStackTrace();

}

}

}

1) set CLASSPATH=C:\Eclipse\Workspace\Working\Computer

2) cd to working and type java -cp . reflect

3) Intens.java file succed to compile but the result :

java.lang.ClassNotFoundException: hardware.Intens

at java.net.URLClassLoader$1.run(Unknown Source)

at java.security.AccessController.doPrivileged(Native Method)

at java.net.URLClassLoader.findClass(Unknown Source)

at java.lang.ClassLoader.loadClass(Unknown Source)

at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)

at java.lang.ClassLoader.loadClass(Unknown Source)

at java.lang.ClassLoader.loadClassInternal(Unknown Source)

at java.lang.Class.forName0(Native Method)

at java.lang.Class.forName(Unknown Source)

at reflect.main(reflect.java:13)

what's wrong?

thanks again...

2Puluh11Lapan3a at 2007-7-14 22:04:25 > top of Java-index,Core,Core APIs...
# 7

As I told you, you need to have both classes' package roots on your classpath.

I'm too lazy to go back and look, but let's say you have

Class A in no package, in directory X/Y, that is, X/Y/A.class (reflect)

Class B in package p, in X/Y/Z/p/B.class (Intens)

This is basically the situation you have, if I recall.

You need to have both X/Y and X/Y/Z on your classpath.

Also, classes that are in a package cannot access classes that are in no package. I don't know if this is an issue for you. This only matters if Intens is trying to access reflect, but I think you're doing it the other way.

jverda at 2007-7-14 22:04:25 > top of Java-index,Core,Core APIs...
# 8
> 1) set> CLASSPATH=C:\Eclipse\Workspace\Working\Computer> > 2) cd to working and type java -cp . reflectThe -cp in #2 makes the VM ignore the classpath environment variable from #1. It does not add to it.
jverda at 2007-7-14 22:04:25 > top of Java-index,Core,Core APIs...
# 9

Hey, thank u very much...now i understand about classloader,package and how to use it..i already solve every my problems...thanks for everythings, you're the best man...if i have another problems (i'm very sure i'm not going to ask about this topic again) can i ask you again? can i know you're email?

thanks again...

2Puluh11Lapan3a at 2007-7-14 22:04:25 > top of Java-index,Core,Core APIs...
# 10

Cool! I'm glad I was able to help.

I don't give out my email. If you have more questions, post them here, and somebody will answer them. There are lots of folks here who can help you as well as or better than I, so sending me an email would be a less effective way for you to get help.

Good luck, and post here again if you get stuck.

jverda at 2007-7-14 22:04:25 > top of Java-index,Core,Core APIs...