Java Compiler

I wrote three programs with the names 'A.java', 'B.java' and 'C.java' (abc package). When i compile

'javac *.java', sometimes it is not compiling the programs though it is not giving any compiler errors. A uses B class.

Please can somebody explain me the reason, why it is not compile those classes.

[334 byte] By [SreedharRacharla] at [2007-9-30 16:19:45]
# 1
Please post a better explanation of "sometimes it is not compiling." How do you know it is not compiling?
atmguy at 2007-7-6 0:00:22 > top of Java-index,Administration Tools,Sun Connection...
# 2
It was not reflecting changes made by me. I put simple System.out.println as the first statement in main function..If i compile the files individually like A.java... it is reflecting the changes made by me.
SreedharRacharla at 2007-7-6 0:00:22 > top of Java-index,Administration Tools,Sun Connection...
# 3

Which version?

Which folder are you compiling from?

If you have an abc package you should compile from the parent folder of abc:

javac ./abc/*.java -d ./

FYI: Ant http://ant.apache.org/ scripts are useful project build tool components well worth learning to use

<target depends="init" name="compile">

<!-- Both srcdir and destdir should be package roots. -->

<mkdir dir="${classes.dir}"/>

<javac debug="true" deprecation="true" destdir="${classes.dir}" encoding="UTF-8" source="1.5" srcdir="${src.dir}">

<compilerarg value="-Xlint:unchecked"/>

<classpath>

<pathelement location="./lib/saaj-api.jar"/>

<pathelement location="./lib/saaj-impl.jar"/>

</classpath>

</javac>

</target>

Many IDE's help template and maintain the Ant scripts such as www.netbeans.org

Martin3 at 2007-7-6 0:00:22 > top of Java-index,Administration Tools,Sun Connection...
# 4

I have below three classes under C:\Project\classes\abc (package name is abc)

A.java

B.java

C.java

Note: A class uses B class internally and B class uses C class.

I compiled using below command from DOS Prompt:

cd c:\Project\abc

javac *.java

It did not give any compilation errors.

(from c:\Project\abc folder)

cd ..

java abc.A

But the changes whatever i have done were reflecting as output (Simple SOP as the first line in main function)

Later i have done like below

cd c:\Project\abc

javac A.java

javac B.java

javac C.java

(from c:\Project\abc folder)

then,

cd ..

java A

Now it is displaying the message whatever i put in SOP

Why for the first time it did not compile when i use *.java ?

SreedharRacharla at 2007-7-6 0:00:22 > top of Java-index,Administration Tools,Sun Connection...
# 5
Try comipling fromC:\Project\classes javac ./abc/*.java -d ./
Martin3 at 2007-7-6 0:00:22 > top of Java-index,Administration Tools,Sun Connection...
# 6

I know it works...but what is the difference.? See below question again...

I have below three classes under C:\Project\abc (package name is abc)

A.java

B.java

C.java

Note: A class uses B class internally and B class uses C class.

I compiled using below command from DOS Prompt:

cd c:\Project\abc

javac *.java

It did not give any compilation errors.

(from c:\Project\abc folder)

cd ..

java abc.A

But the changes whatever i have done were reflecting as output (Simple SOP as the first line in main function)

Later i have done like below

cd c:\Project\abc

javac A.java

javac B.java

javac C.java

(from c:\Project\abc folder)

then,

cd ..

java A

Now it is displaying the message whatever i put in SOP

Why for the first time it did not compile when i use *.java ?

SreedharRacharla at 2007-7-6 0:00:22 > top of Java-index,Administration Tools,Sun Connection...
# 7

You couldn't have compiled A.java from the c:\Project folder without javac ./abc/A.java so your question is unanswerable.

Inside of A.java you have a line

package abc;

Java uses packages for distinguishing classes. And meet the ISO Standard 3166, 1981 namespace standards. Java will compile with any package you come up with but it is good practice to follow namespace conventions. Of course if you don't have a domain name you have to com up with something.

Java will not allow reference to non' packaged classes in packaged classes anymore. It became more strict. to ISO Standard 3166, 1981.

See: http://java.sun.com/docs/codeconv/html/CodeConventions.doc2.html#3043

Martin3 at 2007-7-6 0:00:22 > top of Java-index,Administration Tools,Sun Connection...
# 8

Why can't i compile java programs by going to individual folder. i.e. C:\Project\abc

If it could not compile the program, it would have give the error. And moreover why it is giving proper output when i compile the files individually (Note that i am still at C:\Project\abc folder only).

Here question is why A.java file did not give proper output when i compile using '*.java' (from c:\Project\abc folder) ?

SreedharRacharla at 2007-7-6 0:00:22 > top of Java-index,Administration Tools,Sun Connection...
# 9

What version of Java?

I set up your scenario with a HelloWorld.java with a package of test.

Could not compile from c:/Projects/test with

javac Helloworld.java

gives error

Exception in thread "main" java.lang.NoClassDefFoundError: Helloworld (wrong nam

e: test/Helloworld)

at java.lang.ClassLoader.defineClass1(Native Method)

at java.lang.ClassLoader.defineClass(ClassLoader.java:604)

at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:12

3)

I don't get proper output

Martin3 at 2007-7-6 0:00:22 > top of Java-index,Administration Tools,Sun Connection...
# 10
The HelloWorld was Helloworld in my example...
Martin3 at 2007-7-6 0:00:22 > top of Java-index,Administration Tools,Sun Connection...
# 11
I am using Java 2 SDK, Standard Edition Version 1.4.2
SreedharRacharla at 2007-7-6 0:00:22 > top of Java-index,Administration Tools,Sun Connection...
# 12

I'm using 1.5.x. I recall that the major change was between 1.3 and 1.4 regarding packaging and namespace constraints. I have another box with 1.4 that I will eventually try.

But again I would suggest that you use

javac ./abc/*.java

for you compiles. Also find IDE and tools that assist in managing a project and it's build.

Martin3 at 2007-7-6 0:00:22 > top of Java-index,Administration Tools,Sun Connection...
# 13

I am confused now..At the time of compiling the program why would it gives NoClassDefFoundError Exception. This is runtime exception.

I have below three classes under C:\Project\abc (package name is abc)

A.java

B.java

C.java

Note: A class uses B class internally and B class uses C class.

I compiled using below command from DOS Prompt:

cd c:\Project\abc

javac *.java

It did not give any compilation errors.

(from c:\Project\abc folder)

cd ..

java abc.A

But the changes whatever i have done were reflecting as output (Simple SOP as the first line in main function)

Later i have done like below

cd c:\Project\abc

javac A.java

javac B.java

javac C.java

(from c:\Project\abc folder)

then,

cd ..

java abc.A

Now it is displaying the message whatever i put in SOP

Why for the first time it did not compile when i use *.java ?

SreedharRacharla at 2007-7-6 0:00:22 > top of Java-index,Administration Tools,Sun Connection...
# 14
I am not convinced at all. I know that if i use ../abc/*.java it works fine. But why does it not work if i compile from individual folder? More scary part is, it is not giving any error or warning at the time of compiling the programs.
SreedharRacharla at 2007-7-6 0:00:22 > top of Java-index,Administration Tools,Sun Connection...
# 15

Sorry for the confusion. Tthat was my subsequent run in the same cmd file:

java Helloword.java

that produced that error.

1.5 does compile the Helloworld.java from it's test folder too. It finds the package inside all right. But does not like the *.java.

Again

javac ./abc/*.java

from c:\Projects works exacly as advertised on

http://java.sun.com/j2se/1.5.0/docs/tooldocs/windows/javac.html

Martin3a at 2007-7-19 22:01:32 > top of Java-index,Administration Tools,Sun Connection...
# 16
why '*.java' is not behaving same way as 'A.java' ?
SreedharRacharlaa at 2007-7-19 22:01:32 > top of Java-index,Administration Tools,Sun Connection...
# 17

> why does it not work if i compile from individual

> folder? More scary part is, it is not giving any error

> or warning at the time of compiling the programs.

Are you saying that when you use javac *.java you do not see the changes but when you then compile individually it does show the changes?When I try javac *.java the changes do show - I tried exactly what you posted. I am using SDK 1.4.1 but I don't think that matters.

Either you are doing something that isn't apparent from your posts or something is really wrong with your javac. After you do javac *.java do the dates on the *.class files change?

Also, I noticed in the first post you had c:\Project\classes\abc but in later posts you had c:\Project\abc. Please clarify.

atmguya at 2007-7-19 22:01:32 > top of Java-index,Administration Tools,Sun Connection...
# 18
Even it was apparent for me. why it is behaving differently if i use *.java? Is there any different concept around it?Sorry for the confusion it is c:\Project\abc only.
SreedharRacharlaa at 2007-7-19 22:01:32 > top of Java-index,Administration Tools,Sun Connection...
# 19

I just thought of something. Because you get no errors when you compile from the abc directory, you must have a directory in the Classpath that contains an abc directory. But this does not necessarily mean that the Classpath contains c:\Project. Most likely the compiler and the java.exe launcher are finding a different abc directory with different A classes in them. (Although this does not completely explain why compiling the individual .java files works, unless you are making a copy of the .java files somewhere else.)

atmguya at 2007-7-19 22:01:32 > top of Java-index,Administration Tools,Sun Connection...
# 20
I was not copying the .java files anywhere. It worked if i compile the files individually. I was compiling sames files from the same directory. ClassPath does not have abc directory.
SreedharRacharlaa at 2007-7-19 22:01:32 > top of Java-index,Administration Tools,Sun Connection...
# 21

> I was not copying the .java files anywhere. It worked

> if i compile the files individually. I was compiling

> sames files from the same directory. ClassPath does

> not have abc directory.

To clarify, I said the Classpath must have a directory which contains an abc directory, not that the Classpath has an abc directory.

OK, what happens when you try thisjavac -classpathC:\Project c:\Project\abc\*.java or cd c:\project\abc

javac -classpath c:\Project *.java

atmguya at 2007-7-19 22:01:32 > top of Java-index,Administration Tools,Sun Connection...