7 Replies - 5449 Views - Last Post: 23 April 2011 - 01:04 AM Rate Topic: -----

#1 CarDriver  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 65
  • View blog
  • Posts: 431
  • Joined: 17-February 11

How do I compile a program with multiple classes in cmd?

Posted 22 April 2011 - 02:24 PM

When I try to compile a Java program with more than just the main class in the command prompt, I find that the classes other than the main one compile with no problem, but when I try to compile the main with variables connected to the other classes it doesn't know what the variables are. I compiled the other classes first, and all classes are in a directory together.

Sorry about the wording, I'm not sure what you would call 'classes other than main' and such. I'm clearly pretty new.

Is This A Good Question/Topic? 0
  • +

Replies To: How do I compile a program with multiple classes in cmd?

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8347
  • View blog
  • Posts: 31,910
  • Joined: 06-March 08

Re: How do I compile a program with multiple classes in cmd?

Posted 22 April 2011 - 02:56 PM

> javac *.java

should do the trick
Was This Post Helpful? 1
  • +
  • -

#3 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1820
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: How do I compile a program with multiple classes in cmd?

Posted 22 April 2011 - 02:57 PM

If you are compiling many classes in the same directory then say:
javac *.java


But what I know is that, if you only compile the class with the main method then the dependent classes are compiled together. If not working then try the above command which means every file with .java extension in that directory.
Was This Post Helpful? 1
  • +
  • -

#4 CarDriver  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 65
  • View blog
  • Posts: 431
  • Joined: 17-February 11

Re: How do I compile a program with multiple classes in cmd?

Posted 22 April 2011 - 03:50 PM

That did the trick, but I'm sort of confused about why the compiler can recognise that it is intended for one program by noticing that it is in the same directory when you use *.java but it can't recognise that the already compiled class is part of the main when you try to compile them seperately.
Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8347
  • View blog
  • Posts: 31,910
  • Joined: 06-March 08

Re: How do I compile a program with multiple classes in cmd?

Posted 22 April 2011 - 04:03 PM

View PostAVReidy, on 22 April 2011 - 05:50 PM, said:

That did the trick, but I'm sort of confused about why the compiler can recognise that it is intended for one program by noticing that it is in the same directory when you use *.java but it can't recognise that the already compiled class is part of the main when you try to compile them seperately.

Actually it depends
If every time you modify a that.java you >javac that.java it will work
If you modify 2 or more .java files without re-compiling every change then a > javac *.java is the easiest/laziest not expensive solution
Was This Post Helpful? 0
  • +
  • -

#6 cfoley  Icon User is offline

  • Cabbage
  • member icon

Reputation: 2069
  • View blog
  • Posts: 4,307
  • Joined: 11-December 07

Re: How do I compile a program with multiple classes in cmd?

Posted 22 April 2011 - 06:58 PM

Do you have a cyclic dependency? If two classes refer to each other, this can screw up compiling them separately.
Was This Post Helpful? 0
  • +
  • -

#7 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8347
  • View blog
  • Posts: 31,910
  • Joined: 06-March 08

Re: How do I compile a program with multiple classes in cmd?

Posted 22 April 2011 - 07:23 PM

View Postcfoley, on 22 April 2011 - 08:58 PM, said:

Do you have a cyclic dependency? If two classes refer to each other, this can screw up compiling them separately.

:v:
> javac *.java
will do the trick in any case :^:

separatly won't do it, one will wait for the other
Was This Post Helpful? 0
  • +
  • -

#8 cfoley  Icon User is offline

  • Cabbage
  • member icon

Reputation: 2069
  • View blog
  • Posts: 4,307
  • Joined: 11-December 07

Re: How do I compile a program with multiple classes in cmd?

Posted 23 April 2011 - 01:04 AM

Oh yeah I know that. :) I was trying to get to the bottom of why he couldn't compile them separately.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1