Java error: package 'x' does not exist

12.1k views Asked by At

So I have a file tree in Linux that looks like this:

  • ~/keyboard_warriors/
    • test/
      • ConfigTest.java
    • ConfigParser.class
    • ConfigParser.java

The problem is that when I try to type in Terminal:
javac ConfigTest.java

I get the error:

ConfigTest.java:2: error: package keyboard_warriors does not exist  
import keyboard_warriors.*;  
^

This is really bugging me and I could not find any answers anywhere. If you could solve my problem, I would be a very happy person.

3

There are 3 answers

0
Jerfov2 On BEST ANSWER

I figured out the answer a long time ago but forgot to post it. sorry :). It is actually quite simple. I was trying to compile the classes from inside the test/ folder. What I did was go up to where all the classes were accessible without having to use a absolute path and without using ../ in the path (AKA the keyboard_warriors/ folder. I just compile from there and everything was A-OK. Thanks for the input though, it did help some ;).

1
Makoto On

It's not going to recognize a directory outside of the classpath as being part of the classpath.

What you probably want is to include another folder inside of where your test is being run:

test/
    keyboard_warriors/
        ConfigTest.java
        ConfigParser.java

This also presumes that you have declared these classes to be in the keyboard_warriors classpath, by this:

package keyboard_warriors;

Depending on how you're compiling these classes, you'd have to add it to the classpath with the -cp flag on javac.

0
T.J. Crowder On

Given the structure you've quoted, it would be odd to be importing keyboard_warriors into ConfigTest; from the structure, you'd expect to see package keyboard_warriors; instead, because ConfigTest is in the keyboard_warriors directory.

If ConfigTest isn't in the keyboard_warriors package, it's probably in the wrong directory.

In general: When you're compiling classes, the .java file should be in a directory named for the package it's in. So for instance, if ConfigTest is meant to be in the package, you'd keep your directory/file structure as it is and use

package keyboard_warriors;

...in ConfigTest.java.

If ConfigTest isn't meant to be in that package, you'd probably move it out of that directory; perhaps:

  • ~/keyboard_warriors/
    • test/
      • ConfigTest.java
  • ConfigParser.class
  • ConfigParser.java

...and then import keyboard_warriors.*; would make sense, provided that your home directory is in your classpath.