Advertisement

[java] compiling multiple *.java files in one project

Started by July 03, 2001 12:40 AM
5 comments, last by farmersckn 23 years, 7 months ago
ok, i''ve seen for instance in JBuilder 4 that a new project generates two files, projectName.java, and Frame1.java or something, where projectName is your main class, and Frame1 is your main frame. how does one go about seperating multiple classes into multiple files using the command-line compiler (javac)??? i tried to see if you did something like "import myprojectname.Frame1" but that didn''t work... help??
Yesterday is the past, tomorrow is the future. Today is a gift, that is why we call it the present.
You will have to bo more specific...

two file names or something? doesn''t help me much. How about the file names with their project and class definition lines?

Advertisement
Let''s just say that I have a class with the main method in it. Lets call that MainClass. Then I have a file with the class MainFrame.


/* * file MainClass.java */package myprogram;public class MainClass { public static void main(String[] commandLine) {  MainFrame f = new MainFrame(); }}/* * file MainFrame.java */package myprogram;import javax.swing.*;public class MainFrame extends JFrame { public MainFrame() { }} 

Obviously, this doesn''t do anything. But the deal is, I don''t know how to compile this with the command line compiler (javac).
I tried javac MainClass.java, and then I get undefined symbol when it gets to MainFrame...

How do IDEs do this?

Do you just do javac MainFrame.java, javac MainClass.java?
Do I need to do something like import myprogram.MainFrame? what?
thankyou
Javac *.java
ujhkfkfk
If you compile in this order than everything should work,

1. javac MainFrame.java
2. javac Main.java

The import will only help if you''ve assigned your classes to packages. Packages are defined via a file hierarchy. Source files in the same directory are assumed to be in the same ''package'', so you wouldn''t need to use the import statement.

There used to be an option in one of the SDK versions I used that let you compile one file and all of the other files that it depended on would also be compiled. I don''t see that option anymore in the SDK''s I use now. It was probably too problematic to maintain...

Hope it works out for you,



joeG

joeG
Ok, I''ve got a guess about what your problem is, and I''m betting it''s packages. I noticed that both of your files are in the "myprogram" package. In order for them to compile correctly from the command line the compiler needs to be invoked from the directory corresponding to the base, unnamed, package, or have the classpath set to that directory.

Given the code you posted (which is correct for using multiple classes, although a bit meager on functionality), assume you are the parent directory of the "myprogram" directory. Then do this:

javac myprogram/*.java

The direction of the slash doesn''t really matter for java, and as I do development across platforms for work, I just stick with the forward slash.

Of course, you don''t actually need to compile all the files in the directory. In fact, if you tell the compiler only to compile a limited set of the files, it will try to track down and if necessary, compile, any unknown class references. So just using

javac myprogram/MainClass.java

would also work. No extra command line options are needed... It just works.
Advertisement
Thanks so much everyone. I''m gonna try it right now.
Yesterday is the past, tomorrow is the future. Today is a gift, that is why we call it the present.

This topic is closed to new replies.

Advertisement