Java Programming snafu

Yes, you're correct. The class used by Randomclass should be in directory C:\Java (in subdirectories if the class has a package).


Also... there's always another way. :)

If both class' source code are made by you, you could compile them all in one go. e.g. C:\Java> javac -d . *.java
That way the compiler will resolve dependecies itself... it will compile classes used by others first. Of course, you cannot do that when you don't have all the source code... and that will recompile *all* source code, which could really take a while if your project is huge.


Oh by the way, I think I should tell you about this too: static final members.
Say we have these two classes:

==== ClassOne.java ====
package test;

public class ClassOne {
public static final String MESSAGE = "Hello, world!";

public static void main(String[] args) {
System.out.println(MESSAGE);
}
}
==== End ClassOne.java ====

==== ClassTwo.java ====
package test;

public class ClassTwo {
public static final String MESSAGE = ClassOne.MESSAGE;

public static void main(String[] args) {
System.out.println(MESSAGE);
}
}
==== End ClassTwo.java ====

Then try these:

C:\> javac -d . *.java

C:\> java test.ClassOne
Hello, world!

C:\> java test.ClassTwo
Hello, world!

Now we modify ClassOne.java, changing the constant MESSAGE.

==== Modified ClassOne.java ====
package test;

public class ClassOne {
public static final String MESSAGE = "Goodbye, world.";

public static void main(String[] args) {
System.out.println(MESSAGE);
}
}
==== End Modified ClassOne.java ====

Then test again, but this time, only compile ClassOne.java:

C:\> javac -d . ClassOne.java

C:\> java test.ClassOne
Goodbye, world.

C:\> java test.ClassTwo
Hello, world!

You'll see the data inside ClassTwo isn't updated. That's because when compiling, the compiler translates static final members into literal values (to reduce generated code size and increase performance).

Therefore, if you have some static final members used by more than one classs, you'll have to recompile them all when the static finals have changed.
 
kcwong, now I have a real programming question. In a gui program, the gui class calls the main class in the button clicked method like I was told I was supposed to. But this causes it to be called everytime I press a button, and it instantiates my instance variables so they are reset every button press. But if I move the call of the class to somewhere else, like the gui class constructor or the main method, I get errors, and if I make an if statement with a counter variable to not call it if it's already been called once, it can't find the variable I'm using for the object of the class.

How can I solve this problem? I need the class to be called once or my program won't work.
 
To summarise:

For Windows XP/Win2k

To make it so that you can compile and run java using textpad you must do the following

1.) Once you've installed the JDK 5.0, Go to Environment variables (Control panel < System < Advanced < Environment Variables)

2.) EDIT: Some versions of the JDK (I'm not sure which) create a variable called 'classpath'. Delete this variable, otherwise textpad will fail to find other classes in the same directory as the current file you are working on.

3.) Change the path variable
e.g. Path: C:\Program Files\Common Files\Adobe\AGL --> C:\Program Files\Common Files\Adobe\AGL;C:\Program Files\Java\jdk1.5.0_05\bin\
Note that spaces do not matter (such as Program Files). There were some rumours that they did.

This system allows for compilation of files in the default package (which most people will be using) - ie when your java source files do not have 'package ....' at the top. I am not sure whether this system also works with packages (untested).

This was with: Textpad 4.7.3, WinXP Sp2, JDK 5.0 (similar results on my win2k machine)
 
There're many programs that also expect a "JAVA_HOME" variable.

Taking the example in Fraglord's post, JAVA_HOME should be set to C:\Program Files\Java\jdk1.5.0_05\

And example is Apache Ant.
 
Back
Top Bottom