The Artima Developer Community
Sponsored Link

Java Answers Forum
compile error

14 replies on 1 page. Most recent reply: Jul 16, 2003 9:36 PM by Masood

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 14 replies on 1 page
Masood

Posts: 9
Nickname: shooter
Registered: Jul, 2003

compile error Posted: Jul 13, 2003 9:47 AM
Reply to this message Reply
Advertisement
I have downloaded an e-book on Java by Bruce Eckel from the following website:
http://www.mindview.net/Books/TIJ/

I am unbale to comppile some programs. which are making refence to the declaration in the following package

import com.bruceeckel.simpletest.*;

According the book, everything is available in code directory. But I cant solve this problem. Can anybody help me plz. Thank you


zenykx

Posts: 69
Nickname: zenykx
Registered: May, 2003

Re: compile error Posted: Jul 13, 2003 5:18 PM
Reply to this message Reply
You probably dont set the classpath (to include the out directory, i.e. the directory where the .class files will be) of the javac compiler.

Masood

Posts: 9
Nickname: shooter
Registered: Jul, 2003

Re: compile error Posted: Jul 15, 2003 1:22 AM
Reply to this message Reply
Thanks for your help. You are correct in your answer. I added the correct directory in th Path. But still I am unable to compile this program. let me tell you about the scenario in which directory I am executing this file.


In Java directory, is where my practice programs are saved and from here I compile this program
C:\Java> javac Assignment.java
assignment.java:5: package com.bruceeckel.simpletest does not exist
import com.bruceeckel.simpletest.*;
^
assignment.java:12: cannot resolve symbol
symbol : class Test
location: class Assignment
static Test monitor = new Test();
^
assignment.java:12: cannot resolve symbol
symbol : class Test
location: class Assignment
static Test monitor = new Test();
^
3 errors



When I compile the file from I get 2 errors.
C:\Java\aaa-TIJ3-distribution\TIJ-3rd-edition-code\com\bruceeckel\simpletest javac assignment.java
assignment.java:5: package com.bruceeckel.simpletest does not exist
import com.bruceeckel.simpletest.*;
^
assignment.java:12: cannot access Test
bad class file: .\Test.java file does not contain class Test
Please remove or make sure it appears in the correct subdirectory of the classpath.
static Test monitor = new Test();
^
2 errors

The above directory contains all the class files for this code. I know (at least I hope so) all the code is correct. Its giving me 2 errors. But I dont know how to set Path variables.

I want to compile my programs from C:\Java>
I am lost here. I dont know the structure of import statement. I dont know which directory to add in the path variable


I do not about classpath. I did not find out directory I am editing my autuesec.bat file.

Does com directory have to do anything with this.

Thank you very ymuch

Masood

Posts: 9
Nickname: shooter
Registered: Jul, 2003

Re: compile error Posted: Jul 15, 2003 3:44 AM
Reply to this message Reply
Dont worry about the above reply. I have solved the problem, by setting classpath variable. After that I ran into the problem when I run the program. I receive this error


C:\Java>java assignment
Exception in thread "main" java.lang.NoClassDefFoundError: assignment (wrong name: Assignment)
at java.lang.ClassLoader.defineClass0(Native Method)
at java.lang.ClassLoader.defineClass
ClassLoader.java:502)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:123)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:250)
at java.net.URLClassLoader.access$100(URLClassLoader.java:54)
at java.net.URLClassLoader$1.run(URLClassLoader.java:193)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:186)
at java.lang.ClassLoader.loadClass(ClassLoader.java:299)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:265)
at java.lang.ClassLoader.loadClass(ClassLoader.java:255)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:315)

Plz help.
Thank you

Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: compile error Posted: Jul 15, 2003 11:27 AM
Reply to this message Reply
Looks like you are mixing cases between file name and class name; you should either fastidiously use "Assignment" or always use "assignment," but not whimsically use both.

Masood

Posts: 9
Nickname: shooter
Registered: Jul, 2003

Re: compile error Posted: Jul 15, 2003 1:59 PM
Reply to this message Reply
Ok I got it. followed the directoins from java.su.com. Now I understand that names are case sensitive. Now I am getting the following error.


C:\Java>java -classpath c:\code Assignment
Exception in thread "main" java.lang.NoClassDefFoundError: Assignment


How can I set the classpath variable permanently?


I compiled the program by the following command and it compiled :)

C:\Java>javac -classpath c:\code Assignment.java


This is one of those stupid mistakes that has taken 5hrs of my time.
Thank you for your help. I am trying to learn Java.

Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: compile error Posted: Jul 15, 2003 4:30 PM
Reply to this message Reply
Don't feel too bad. I think this is a stupid mistake of the Java compiler that it can't give helpful messages for the most common errors people make, instead of cryptic messages that just frustrate and confound beginners. It is pretty silly that javac requires that the file be specified with the ".java" and java requires that the class not have a ".java" and they are both too stupid to either infer the correct thing to do, or at least give a meaningful message. Jikes is better than javac on this count, but that only helps with half the equation.

As far as your classpath goes, you need to set the environment variable. Based on the evidence you've shown, I guess you are using Windows. If it is pre-XP, then you need to add a line to autoexec.bat. If it is XP/Win2k you can go to the "System Properties" (right-click My Computer (no, I mean your computer!) and choose "properties") and on the Advanced tab click the "Environment Variables" button; from there you can add the classpath variable to your system variables.

Masood

Posts: 9
Nickname: shooter
Registered: Jul, 2003

Re: compile error Posted: Jul 15, 2003 6:11 PM
Reply to this message Reply
I am using win98. I dont know which command to add in autoexec.bat. I have PATH=c:\windows\;......other dir.....c:\code

My still stuck on that error. import com.bruceeckel.simpletest.*;

The structure of my CODE directory is as follows:=

code
|
com
|
bruceeckel
|
simpletest




C:\Java>javac -classpath C:\code Assignment.java

C:\Java>java -classpath C:\code Assignment
Exception in thread "main" java.lang.NoClassDefFoundError: Assignment


The program compiles using javac. So classpath command is working. But when I run the program using java. I get that error. I am thinking where else compiler is looking, for whatever that it needs to find out.

Can you guys recommend a different compiler. I will try to compile this program using a diff. compiler now.
Thanks for your help.

Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: compile error Posted: Jul 15, 2003 6:28 PM
Reply to this message Reply
Add a line to your autoexec.bat that looks like this:

set classpath=c:\code;.


You can type that on the command line of your DOS box, too, so you don't have to reboot (but after you reboot, it will no longer be necessary to type it in the future).

Let's see your main() method in the Assignment class. If it isn't defined correctly, that could be the reason the JVM isn't finding it. It should look something like this:
class Assignment
{
   public static void main( String [] args )
   {
      System.out.println( "What the hell, oh world?" );
   }
}

If the case isn't right (eg. "Main"), or it isn't static, or for any other reason isn't found, then you get that silly error message that you are getting.

Masood

Posts: 9
Nickname: shooter
Registered: Jul, 2003

Re: compile error Posted: Jul 15, 2003 8:57 PM
Reply to this message Reply
Here is the code that I am trying to compile. Assignment class is calling (or refrencing) class Test. Which is in a c:\code directroy. I have also included the code for Test.java

I hope this helps you guys. Thanks for your help.

//: c03:Assignment.java
// Assignment with objects is a bit tricky.
// From 'Thinking in Java, 3rd ed.' (c) Bruce Eckel 2002
// www.BruceEckel.com. See copyright notice in CopyRight.txt.
import com.bruceeckel.simpletest.*;

class Number {
int i;
}

public class Assignment {
static Test monitor = new Test();
public static void main(String[] args) {
Number n1 = new Number();
Number n2 = new Number();
n1.i = 9;
n2.i = 47;
System.out.println("1: n1.i: " + n1.i +
", n2.i: " + n2.i);
n1 = n2;
System.out.println("2: n1.i: " + n1.i +
", n2.i: " + n2.i);
n1.i = 27;
System.out.println("3: n1.i: " + n1.i +
", n2.i: " + n2.i);
monitor.expect(new String[] {
"1: n1.i: 9, n2.i: 47",
"2: n1.i: 47, n2.i: 47",
"3: n1.i: 27, n2.i: 27"
});
}
}


**********************************************************

//: com:bruceeckel:simpletest:Test.java
// Simple utility for testing program output. Intercepts
// System.out to print both to the console and a buffer.
// From 'Thinking in Java, 3rd ed.' (c) Bruce Eckel 2002
// www.BruceEckel.com. See copyright notice in CopyRight.txt.
package com.bruceeckel.simpletest;
import java.io.*;
import java.util.*;
import java.util.regex.*;

public class Test {
// Bit-shifted so they can be added together:
public static final int
EXACT = 1 << 0, // Lines must match exactly
AT_LEAST = 1 << 1, // Must be at least these lines
IGNORE_ORDER = 1 << 2, // Ignore line order
WAIT = 1 << 3; // Delay until all lines are output
private String className;
private TestStream testStream;
public Test() {
// Discover the name of the class this
// object was created within:
className =
new Throwable().getStackTrace()[1].getClassName();
testStream = new TestStream(className);
}
public static List fileToList(String fname) {
ArrayList list = new ArrayList();
try {
BufferedReader in =
new BufferedReader(new FileReader(fname));
try {
String line;
while((line = in.readLine()) != null) {
if(fname.endsWith(".txt"))
list.add(line);
else
list.add(new TestExpression(line));
}
} finally {
in.close();
}
} catch (IOException e) {
throw new RuntimeException(e);
}
return list;
}
public static List arrayToList(Object[] array) {
List l = new ArrayList();
for(int i = 0; i < array.length; i++) {
if(array instanceof TestExpression) {
TestExpression re = (TestExpression)array;
for(int j = 0; j < re.getNumber(); j++)
l.add(re);
} else {
l.add(new TestExpression(array.toString()));
}
}
return l;
}
public void expect(Object[] exp, int flags) {
if((flags & WAIT) != 0)
while(testStream.numOfLines < exp.length) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
List output = fileToList(className + "Output.txt");
if((flags & IGNORE_ORDER) == IGNORE_ORDER)
OutputVerifier.verifyIgnoreOrder(output, exp);
else if((flags & AT_LEAST) == AT_LEAST)
OutputVerifier.verifyAtLeast(output,
arrayToList(exp));
else
OutputVerifier.verify(output, arrayToList(exp));
// Clean up the output file - see c06:Detergent.java
testStream.openOutputFile();
}
public void expect(Object[] expected) {
expect(expected, EXACT);
}
public void expect(Object[] expectFirst,
String fname, int flags) {
List expected = fileToList(fname);
for(int i = 0; i < expectFirst.length; i++)
expected.add(i, expectFirst);
expect(expected.toArray(), flags);
}
public void expect(Object[] expectFirst, String fname) {
expect(expectFirst, fname, EXACT);
}
public void expect(String fname) {
expect(new Object[] {}, fname, EXACT);
}
} ///:~

Masood

Posts: 9
Nickname: shooter
Registered: Jul, 2003

Re: compile error Posted: Jul 15, 2003 9:03 PM
Reply to this message Reply
Here is the code that I am trying to compile. Assignment class is calling (or refrencing) class Test. Which is in a c:\code directroy. I have also included the code for Test.java

If I add set classpath=c:\code;. Do I still have to compile using the follwoing commands

C:\java>javac -classpath c:\code Assignment.java
C:\java\java -classpath c:\code Assignment



I tried to compile using w/o classpath option and I got an error. Then whats point of setting classpath in autoexec.bat!!!

I hope this helps you guys. Thanks for your help.

//: c03:Assignment.java
// Assignment with objects is a bit tricky.
// From 'Thinking in Java, 3rd ed.' (c) Bruce Eckel 2002
// www.BruceEckel.com. See copyright notice in CopyRight.txt.
import com.bruceeckel.simpletest.*;

class Number {
int i;
}

public class Assignment {
static Test monitor = new Test();
public static void main(String[] args) {
Number n1 = new Number();
Number n2 = new Number();
n1.i = 9;
n2.i = 47;
System.out.println("1: n1.i: " + n1.i +
", n2.i: " + n2.i);
n1 = n2;
System.out.println("2: n1.i: " + n1.i +
", n2.i: " + n2.i);
n1.i = 27;
System.out.println("3: n1.i: " + n1.i +
", n2.i: " + n2.i);
monitor.expect(new String[] {
"1: n1.i: 9, n2.i: 47",
"2: n1.i: 47, n2.i: 47",
"3: n1.i: 27, n2.i: 27"
});
}
}


**********************************************************

//: com:bruceeckel:simpletest:Test.java
// Simple utility for testing program output. Intercepts
// System.out to print both to the console and a buffer.
// From 'Thinking in Java, 3rd ed.' (c) Bruce Eckel 2002
// www.BruceEckel.com. See copyright notice in CopyRight.txt.
package com.bruceeckel.simpletest;
import java.io.*;
import java.util.*;
import java.util.regex.*;

public class Test {
// Bit-shifted so they can be added together:
public static final int
EXACT = 1 << 0, // Lines must match exactly
AT_LEAST = 1 << 1, // Must be at least these lines
IGNORE_ORDER = 1 << 2, // Ignore line order
WAIT = 1 << 3; // Delay until all lines are output
private String className;
private TestStream testStream;
public Test() {
// Discover the name of the class this
// object was created within:
className =
new Throwable().getStackTrace()[1].getClassName();
testStream = new TestStream(className);
}
public static List fileToList(String fname) {
ArrayList list = new ArrayList();
try {
BufferedReader in =
new BufferedReader(new FileReader(fname));
try {
String line;
while((line = in.readLine()) != null) {
if(fname.endsWith(".txt"))
list.add(line);
else
list.add(new TestExpression(line));
}
} finally {
in.close();
}
} catch (IOException e) {
throw new RuntimeException(e);
}
return list;
}
public static List arrayToList(Object[] array) {
List l = new ArrayList();
for(int i = 0; i < array.length; i++) {
if(array instanceof TestExpression) {
TestExpression re = (TestExpression)array;
for(int j = 0; j < re.getNumber(); j++)
l.add(re);
} else {
l.add(new TestExpression(array.toString()));
}
}
return l;
}
public void expect(Object[] exp, int flags) {
if((flags & WAIT) != 0)
while(testStream.numOfLines < exp.length) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
List output = fileToList(className + "Output.txt");
if((flags & IGNORE_ORDER) == IGNORE_ORDER)
OutputVerifier.verifyIgnoreOrder(output, exp);
else if((flags & AT_LEAST) == AT_LEAST)
OutputVerifier.verifyAtLeast(output,
arrayToList(exp));
else
OutputVerifier.verify(output, arrayToList(exp));
// Clean up the output file - see c06:Detergent.java
testStream.openOutputFile();
}
public void expect(Object[] expected) {
expect(expected, EXACT);
}
public void expect(Object[] expectFirst,
String fname, int flags) {
List expected = fileToList(fname);
for(int i = 0; i < expectFirst.length; i++)
expected.add(i, expectFirst);
expect(expected.toArray(), flags);
}
public void expect(Object[] expectFirst, String fname) {
expect(expectFirst, fname, EXACT);
}
public void expect(String fname) {
expect(new Object[] {}, fname, EXACT);
}
} ///:~

Raj Verma

Posts: 1
Nickname: bobsta
Registered: Jul, 2003

Re: compile error Posted: Jul 16, 2003 1:13 AM
Reply to this message Reply
Hi masood,

Just wanted to know if you have set the path including the the file called "bin"?Or else, try saving your .java file in the this folder (bin) then in dos go to the bin folder that contains you java file and try compiling the same way ie:

javac <javafilename.java>
then
java <filename>

Plus I noticed in your code you have declared an object of Test before main.Not sure if that works!

Masood

Posts: 9
Nickname: shooter
Registered: Jul, 2003

Re: compile error Posted: Jul 16, 2003 7:20 AM
Reply to this message Reply
I moved the file Assignment.java and Assignment.class in the directory where it can import Test class. You can see the results. I guess I cant get away with classpath oprion.


C:\Code\com\bruceeckel\simpletest>javac Assignment.java
Assignment.java:5: package com.bruceeckel.simpletest does not exist
import com.bruceeckel.simpletest.*;
^
Assignment.java:12: cannot access Test
bad class file: .\Test.class
class file contains wrong class: com.bruceeckel.simpletest.Test
Please remove or make sure it appears in the correct subdirectory of the classpath.
static Test monitor = new Test();
^
2 errors

C:\Code\com\bruceeckel\simpletest>javac -classpath c:\code Assignment.java

C:\Code\com\bruceeckel\simpletest>java -classpath c:\code Assignment
Exception in thread "main" java.lang.NoClassDefFoundError: Assignment

C:\Code\com\bruceeckel\simpletest>


Results when I compile from bin directory:=

C:\j2sdk1.4.2\bin>javac Assignment.java
Assignment.java:12: cannot access Test
bad class file: .\Test.java
file does not contain class Test
Please remove or make sure it appears in the correct subdirectory of the classpath.
static Test monitor = new Test();
^
1 error


C:\j2sdk1.4.2\bin>javac -classpath c:\code Assignment.java

C:\j2sdk1.4.2\bin>java -classpath c:\code Assignment
Exception in thread "main" java.lang.NoClassDefFoundError: Assignment



At this point I dont know whats going on. I am totally relying on your messages. This is crazy...damn compputers

Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: compile error Posted: Jul 16, 2003 3:52 PM
Reply to this message Reply
You shouldn't be working in the com\bruceeckel\simpletest sub-directory. That directory should only contain modules which are in the com.bruceeckel.simpletest package, which you are not building. Instead, the java files you create should be in the c:\code directory and you should compile them there.

The reason what you are doing doesn't work as you expect is this:
While looking for classes, java goes through all the directories listed in the classpath. For each directory, it will look for package-less (or default package) classes there. Then it will look in package sub-directories for classes in named packages. The package sub-directories it seeks are based on the package name with the dots replaced by the OS path separator ('/' on Unix/Linux, '\' on DOS/Windows). For example, the package com.bruceeckel.simpletest should be in the sub-directory (of any directory specified in the classpath) com\bruceeckel\simpletest.

Since you haven't defined your classpath (or added c:\code to it), as previously suggested, it has defaulted to the current directory only. Since your current directory is c:\code\com\bruceeckel\simpletest, it is looking for c:\code\com\bruceeckel\simpletest\com\bruceeckel\simpletest to find the classes in the com.bruceeckel.simpletest package!

So, you can work from the c:\code directory, as suggested above (which is probably a good idea) without setting the classpath and things will work. In larger projects, you want to be able to work from many different directories and in such a case, you'd want the root of your commonly-used packages to be in your classpath. In that case, you'd add c:\code to your classpath, then you could use the com.bruceeckel.simpletest package, even when your current directory is not c:\code.


By the way, when posting code, please, please use the [java] tags around it! (See the "Formatting Your Post" box in gray to the right, when you are posting).

Masood

Posts: 9
Nickname: shooter
Registered: Jul, 2003

Re: compile error Posted: Jul 16, 2003 9:36 PM
Reply to this message Reply
Thank you very much Matt for solving my problem. I moved my files to the c:\code directory and compiled the program it works and printed the output. I used javac , java commands.

I still have doubts about classpath option. I have added set classpath=c:\code;. in my autoexec.bat. Then I compile the program from c:\java directory. Program compiles but I cannot run. It gives me the same error.

C:\Java>javac -classpath c:\code Assignment.java

C:\Java>java -classpath c:\code Assignment
Exception in thread "main" java.lang.NoClassDefFoundError: Assignment


Why it is giving me the error when I have specifed the correct directory in my autoexec.bat and also when I use java command.

Thank you everybody in helping me out.

Flat View: This topic has 14 replies on 1 page
Topic: Garbage Collection Previous Topic   Next Topic Topic: interview questions

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use