How to i run my program if the sourcode is located in multiple .class files. I can compile them all using javac *.java, but how to i run them?
Printable View
How to i run my program if the sourcode is located in multiple .class files. I can compile them all using javac *.java, but how to i run them?
You don't "run" a class file. There is usually one class with a public static void main method. That is the one you start using java MyMainClass. All the other class files are used by your main class and all the other classes that you instantiate. The classloader will take care of reading the .class files.
Well then i have problem with my main. it says Cannot find or load the main class.
code is located here. http://www.java-forums.org/new-java/...tml#post316548. I fixed the cannot find smbol so thats not the problem anymore.
[CODE]package donut;
import javax.swing.JFrame;
public class Donut extends JFrame {
public Donut() {
add(new Board());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(360, 310);
setLocationRelativeTo(null);
setTitle("Donut");
setVisible(true);
}
public static void main(String[] args) {
new Donut();
}
}[CODE]
As you can see i have the public static void main in here. And if i run this it sais cannot load or find mind .class
The errors are constantly changing now im getting
Exception in thread "main" java.lang.NoClassDefFoundError: Donut (wrong name: do
nut/Donut)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknow n Source)
at java.net.URLClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.access$100(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
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 sun.launcher.LauncherHelper.checkAndLoadMain(Unkno wn Source)
Your code example runs fine here. What is the commandline you are using to start you main class?
java Donut. My suspicion is that i havent gotten the JDK propely installed. i added the java folder in the PATH in the global variables. Anything else?
You should add the packagename as well, so the proper command is java donut.Donut
C:\Users\Marko\Desktop\java>java donut.Donut
Error: Could not find or load main class donut.Donut
Unless you added C:\Users\Marko\Desktop\java to the CLASSPATH environment variable, java.exe will still not find the classes you are trying to feed it. Try java -cp . donut.Donut
-cp . tells java that it needs to find the classes in the current directory.
C:\Users\Marko\Desktop\java>java -cp . donut.Donut
Error: Could not find or load main class donut.Donut
Are the compiled classes in C:\Users\Marko\Desktop\java or are those only the source files? The command I gave you must be run from the directory where all the class files are.
When i run the javac command it creates the .class files in the same folder. I think i might have installed JDK wrongly. I downloaded JDK6. I installed it and then i added the C:/program files .... JDK.... To the Path. Is there anything else i must do?
Is the Donut class REALLY in the donut subdirectory? And do you run the java command from the root of your project, so from C:\Users\Marko\Desktop\java and not C:\Users\Marko\Desktop\java\donut?
there is no subdirectory called donut. the java folder contains few things. a folder called new folder inside there is another .class file that i couldnt get running becaus of the same problem. other things are Board.java, Board.class and Donut.java , Donut.class
In your code you wrote package donut;. That means it should be in a subdirectory donut. The Donut.java file needs to be in that directory. So it ends up here: C:\Users\Marko\Desktop\java\donut\Donut.java
So i created a folder and named it donut. I placed Donut.java in it. But i still get the same error
Compiled? Do a dir /s in C:\Users\Marko\Desktop\java and show what's there.