I've been trying to work out this error with packages, changing the classpath name in a number of different ways (using set, declaring it in the javac command), but I always get the following errors when I try to compile the following code:

import kalut.*;
import java.lang.Math.*;


public class Koetesti {
public static void main(String[] args) {
Koe a = new Koe();


a.x = 5;


System.out.println("a.x: " + a.x);
}
}


ku-hupnet193-75:~/lahdekoodit/javaohj dshaw$ javac Koetesti.java
Koetesti.java:1: package kalut does not exist
import kalut.*;
^
Koetesti.java:6: cannot access Koe
bad class file: ./Koe.java
file does not contain class Koe
Please remove or make sure it appears in the correct subdirectory of the classpath.
Koe a = new Koe();
^
2 errors

It seems as if the it's looking for the class file in the current direction, despite the fact that I have the classpath set to ./classes (more specifically, /Users/dshaw/lahdekoodit/javaohj/classes). It almost seems like I have some stupid error like incorrect case but I don't, still nothing helps. What am I not doing?

Edit: I realised that it's the .java file, not the .class file, that the compiler uses for packages. I also noticed that the compiler wants the package files to be in a directory that is named after the package, in my case, in the directory "kalut". But for some reason it only works when folder "kalut" is in the current working directory, not the "./classes" directory as I have the classpath defined. Any reasons for this?

Edited 3 Years Ago by happygeek: fixed formatting

you need to add the current directory to the classpath as well.
It's not there by default on most modern operating systems for security reasons.

How do you add the current directory to the classpath?

This article has been dead for over six months. Start a new discussion instead.