views:

55

answers:

3

Hey, I wrote a Java program that has 3 classes. When, I use javac, I am getting errors whenever my main class attempts to interact with the other classes. Is there anything special that I need to do? I am just calling javac Main.java. Any help would be greatly appreciated.

Edit:

    DFA myDFA = new DFA();
    String test = args[0];
    if(myDFA.accept(test))

and the error is:

Main.java:19: cannot find symbol symbol: class DFA location class dfa.Main

^ I have 3 of those errors

+3  A: 

Yes, you need to specify the classpath using the -classpath option on javac when you compile.

Try compiling like this:

javac -classpath . *.java

Note the 'dot' after -classpath. It tells the compiler to look in the current directory to find any .java files that it needs.

If you need other paths or JARs, you have to make sure that they appear in the -classpath as well.

duffymo
Not necessarily, not if (for instance) his classpath already contains "."
T.J. Crowder
@T.J: Extract from question: *"I am just calling javac Main.java"* No, s/he likely didn't specify it.
BalusC
Based on the evidence at hand, I'd suspect that s/he didn't specify classpath at all.
duffymo
I did not specify classpath. This helped greatly. Thanks!
PFranchise
Also, if you don't mind answering, how can I now pass something to my main method when compiling it this way? Do I compile then pass, or pass while compiling?
PFranchise
Arguments are passed to main at runtime, not compile time.
duffymo
@duffymo Thanks! I am pretty sure this is two days in a row that you have helped me out. I appreciate it sir.
PFranchise
@BalusC: My point was that javac uses the `CLASSPATH` environment variable by default. If that environment variable includes ".", the current directory is in the classpath even if you don't specify `-classpath` on the javac command line.
T.J. Crowder
Best not to have a CLASSPATH environment variable. Anyone who relies on such a construct is making a mistake. Besides, the evidence before you suggests that there's a problem, and PFranchise's response also tells you that the medicine given has cured the patient. Perhaps you need to learn something about classpath as well.
duffymo
@T.J: true, but the whole question/problem indicates otherwise :)
BalusC
@duffymo: Er, care to tone it down a bit? I commented, BalusC (whom I have a lot of time for) commented on my comment, and I responded to him. As to your points: Whatever. I've been programming and using Java for 14 years without running into issues with using a CLASSPATH environment variable. Development != deployment
T.J. Crowder
@BalusC: As of my comment, there was a lot of room for interpretation about what was going wrong. In any case, I'm just flagging up that you don't *necessarily* need to specify a classpath on the command line. That's all. Not seeing why that's so controversial.
T.J. Crowder
Not much room for interpretation. The diagnosis and the remedy appear to both be spot on. Why do you continue to argue?
duffymo
I've been programming Java only since 1998, but none of the machines that I work on have a CLASSPATH environment variable set. I find it's a crutch for people who don't know how CLASSPATH works. That's why I advise people against having one.
duffymo
+2  A: 

You need to to compile the classes indivdually i.e. javac class1.java javac class2.java javac class2.java

etc.

and then execute as

java cp . MainClass.Main

Raj
A: 

first, use an IDE. don't do cmd line.

if you use javac, you should give it all source files that should be compiled

javac Main.java DFA.java ... 

javac *.java

javac -sourcepath .  Main.java 

again, get an IDE, don't do cmd line.

irreputable
I usually use an IDE, however my prof requires that he can test my code this way, so I am having to test it this way.
PFranchise