Jump to content

Compile Application Multiple Classes

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
6 replies to this topic

#1
MarkTheSpark

MarkTheSpark

    Newbie

  • Members
  • Pip
  • 4 posts
Hello,

I'm using NetBeans 6.5 and would like to know how to compile multiple classes at the same time. When creating a new project I'm using these settings:

New Java Application

Name and Location
__________________________________________________

Project Name: GradeBook

Project Location: C:\Documents and Settings\Mark\My Documents\NetBeansProjects

Project Folder: C:\Documents and Settings\Mark\My Documents\NetBeansProjects (grayed out)

(unchecked) Use Dedicated Folders for Storing Libraries

(checked) Create Main Class gradebook.GradeBook

(checked) Set as Main Project

I tried using C:\Documents and Settings\Mark\My Documents\NetBeansProjects>javac GradeBook.java GradeBookTest.java at the MS-DOS prompt but I got 'javac' is not recognized as an internal or external command, operable program or batch file. The book I'm using is based on Java SE 6 but I was having trouble setting the path variable so someone told me NetBeans was easier to use.

Another thing that confuses me is how you can compile the class if it doesn't contain the class 'main'. The book I'm using, "Java How To Program" 7th Ed. by Deitel and Deitel gives an example on page 87.

In NetBeans I entered:

package gradebook;


// Fig. 3.1: GradeBook.java

// Class declaration with one method.


public class GradeBook

{

// display a welcome method to the GradeBook user

public void displayMessage()

{

System.out.println("Welcome to the Grade Book!");

} // end method displayMessage


} // end class GradeBook


and

package gradebooktest;


// Fig. 3.2: GradeBookTest.java

// Create a GradeBook object and call its displayMessage method.


public class GradeBookTest

{

// main method begins program execution

public static void main( String args[] )

{

// create a GradeBook object and assign it to myGradeBook

GradeBook myGradeBook = new GradeBook();


// call myGradeBook's displayMessage method

myGradeBook.displayMessage();

} // end main


} // end class GradeBookTest


Any help would be greatly appreciated!

Regards,

Mark

#2
Sinipull

Sinipull

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 386 posts
Am i the only one, who finds incredibly hard to read the code, when it's over commented?

main method is a method, where program starts to execute the program, so you only need it in one class, because all the other classes are called by running this method. Exactly like in your example, where Gradebook class, which doesn't have main method, is being called from GradeBookTest class, which is the "beginning" of the program.




package gradebooktest;

public class GradeBookTest{
      public static void main( String args[] ){
             GradeBook myGradeBook = new GradeBook();
             myGradeBook.displayMessage();
      } 
}

package gradebook;

public class GradeBook{
      public void displayMessage(){
            System.out.println("Welcome to the Grade Book!");
      } 
} 

That's what a code should look like, so you could understand it without actually reading it..

#3
MarkTheSpark

MarkTheSpark

    Newbie

  • Members
  • Pip
  • 4 posts
Thanks for the reply and the lesson on the main method. If the code I submitted is over-commented you can express your disdain to the authors of the book I'm using. I still don't understand why you have to compile the source files simultaneously and how that can be accomplished in NetBeans 6.5. Are there any Java programmers in the Orlando, FL area who would be willing to help me with my programming? I'm a junior at the University of Central Florida so maybe we could meet on campus.

#4
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
Does it not automatically compile everything once you want to run the program? That's at least what it does with 'IntelliJ' (pay to play program I use with licence from school) I mean the main is in the class GradeBookTest now. But if you type an error in GradeBook. Won't he see it and tell you so?

There is this little program BlueJ, (totally sucks for programming i think but..) In BlueJ you can create classes for java. Write all your code there with no main. For every class you get an extra class in the automatically created class diagram. You can manually compile every single class without having to run the program. Then you can choose 1 class and run that one. it will create an instance of that class. If you right click on that instance icon, you can choose any public method that you've made and manually "activate" that method. If the method requires parameters a popup box opens up where you put in the parameters. Maybe nice to test classes/methods without having to create a main and call every method from there.
Why did i say it sucks? Well, it has no code completion, if you type something wrong, like "string" instead of "String", it does not get underlined in red. If you press the "compile" button you will have to go trough every fault you've made.


Edit: Yes I also don't look overcommented code. But that's what they like to do in books to make it more clear. Even for the most simple code there is :(

#5
MarkTheSpark

MarkTheSpark

    Newbie

  • Members
  • Pip
  • 4 posts
Hello,

Thanks a lot for the reply! I just posted a similar question on NetBeans Forums since it is their software. I'm also going to start attending local Java User's Group meetings. Best of luck with your programming!

Regards,

Mark

#6
ZekeDragon

ZekeDragon

    Writes binary right handed and hex left handed

  • Moderators
  • 2,103 posts
Netbeans does include each source file in a build. I have a question though, in your above code you didn't include "import gradebook.GradeBook;" at the top of your main source file, and that might be the root of all your problems. :)

Or you can put them all under the same package, IE change "package gradebook;" to "package gradebooktest;" and things should be peachy. Though, if you did include that above line of code, don't know yet.

To get Javac running from the command line (trust me, you'll want it), you need to set your path variable in the DOS prompt to point to where Javac and Java are. That's the only problem.
Wow I changed my sig!

#7
MarkTheSpark

MarkTheSpark

    Newbie

  • Members
  • Pip
  • 4 posts
Thanks for the reply Zeke! I'm using NetBeans because I couldn't figure out how to set the path variable when I had Java SE 6 on my computer. NetBeans automatically inserted the package names you see in my code; the examples I used from my book didn't include them. I used the NetBeans Quick Start Guide to set up my two projects and I don't understand what all of the settings do. Getting back to Java basics, why do you have to compile the files simultaneously? I figure as long as both files exist in the same directory there won’t be a problem.