Page 1 of 2 12 LastLast
Results 1 to 20 of 24
  1. #1
    vastrolorde is offline Senior Member
    Join Date
    Jan 2013
    Posts
    124
    Rep Power
    0

    Default source code in multiple .class files

    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?

  2. #2
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    911
    Rep Power
    2

    Default Re: source code in multiple .class files

    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.

  3. #3
    vastrolorde is offline Senior Member
    Join Date
    Jan 2013
    Posts
    124
    Rep Power
    0

    Default Re: source code in multiple .class files

    Well then i have problem with my main. it says Cannot find or load the main class.

    code is located here. cannot find symbol. I fixed the cannot find smbol so thats not the problem anymore.

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: source code in multiple .class files

    Quote Originally Posted by vastrolorde View Post
    code is located here. cannot find symbol. I fixed the cannot find smbol so thats not the problem anymore.
    And don't you think you should mention that in the other thread, so that people don't keep trying to help with a problem that's already solved?

    Also, since this is a forum, we would expect you to share your solution for the benefit of the community.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: source code in multiple .class files

    Quote Originally Posted by vastrolorde View Post
    Well then i have problem with my main. it says Cannot find or load the main class.
    SurfMan's already addressed that issue. What don't you understand in post #2?

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  6. #6
    vastrolorde is offline Senior Member
    Join Date
    Jan 2013
    Posts
    124
    Rep Power
    0

    Default Re: source code in multiple .class files

    [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

  7. #7
    vastrolorde is offline Senior Member
    Join Date
    Jan 2013
    Posts
    124
    Rep Power
    0

    Default Re: source code in multiple .class files

    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)

  8. #8
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    911
    Rep Power
    2

    Default Re: source code in multiple .class files

    Your code example runs fine here. What is the commandline you are using to start you main class?

  9. #9
    vastrolorde is offline Senior Member
    Join Date
    Jan 2013
    Posts
    124
    Rep Power
    0

    Default Re: source code in multiple .class files

    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?
    Last edited by vastrolorde; 01-03-2013 at 04:33 PM.

  10. #10
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    911
    Rep Power
    2

    Default Re: source code in multiple .class files

    You should add the packagename as well, so the proper command is java donut.Donut

  11. #11
    vastrolorde is offline Senior Member
    Join Date
    Jan 2013
    Posts
    124
    Rep Power
    0

    Default Re: source code in multiple .class files

    C:\Users\Marko\Desktop\java>java donut.Donut
    Error: Could not find or load main class donut.Donut

  12. #12
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    911
    Rep Power
    2

    Default Re: source code in multiple .class files

    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.

  13. #13
    vastrolorde is offline Senior Member
    Join Date
    Jan 2013
    Posts
    124
    Rep Power
    0

    Default Re: source code in multiple .class files

    C:\Users\Marko\Desktop\java>java -cp . donut.Donut
    Error: Could not find or load main class donut.Donut

  14. #14
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    911
    Rep Power
    2

    Default Re: source code in multiple .class files

    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.

  15. #15
    vastrolorde is offline Senior Member
    Join Date
    Jan 2013
    Posts
    124
    Rep Power
    0

    Default Re: source code in multiple .class files

    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?

  16. #16
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    911
    Rep Power
    2

    Default Re: source code in multiple .class files

    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?

  17. #17
    vastrolorde is offline Senior Member
    Join Date
    Jan 2013
    Posts
    124
    Rep Power
    0

    Default Re: source code in multiple .class files

    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

  18. #18
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    911
    Rep Power
    2

    Default Re: source code in multiple .class files

    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

  19. #19
    vastrolorde is offline Senior Member
    Join Date
    Jan 2013
    Posts
    124
    Rep Power
    0

    Default Re: source code in multiple .class files

    So i created a folder and named it donut. I placed Donut.java in it. But i still get the same error

  20. #20
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    911
    Rep Power
    2

    Default Re: source code in multiple .class files

    Compiled? Do a dir /s in C:\Users\Marko\Desktop\java and show what's there.

Page 1 of 2 12 LastLast

Similar Threads

  1. read source code from a class
    By argus in forum New To Java
    Replies: 6
    Last Post: 10-14-2010, 07:05 PM
  2. Project containing multiple source files
    By mr smiley in forum Eclipse
    Replies: 0
    Last Post: 04-25-2009, 10:31 PM
  3. How to Compile JavaScript source into Java class files
    By tosreejith in forum New To Java
    Replies: 0
    Last Post: 04-06-2009, 03:12 PM
  4. multiple class files
    By nemesys571 in forum Eclipse
    Replies: 0
    Last Post: 06-26-2008, 01:58 AM
  5. Organize class source code
    By Alejandro Valdez in forum Eclipse
    Replies: 0
    Last Post: 05-16-2008, 02:58 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •