0

I have two java programs (classes), both in a directory called java_weirdness.

Here they are below :

MyClass

package java_weirdness;
public class MyClass{
    public int num;
    public MyClass(int initialNum) {
        num = initialNum;
    }
}

MyLauncher

package java_weirdness;

public class MyLauncher {

    public static void main(String[] args) {
        MyClass new_member = new MyClass(3);
    }
}

When I compile MyLauncher.java, I get the following errors, even though I clearly imported it:

MyLauncher.java:7: error: cannot find symbol
        MyClass new_member = new MyClass(3);
        ^
  symbol:   class MyClass
  location: class MyLauncher
MyLauncher.java:7: error: cannot find symbol
        MyClass new_member = new MyClass(3);
                                 ^
  symbol:   class MyClass
  location: class MyLauncher
2 errors

What is the proper way to access MyClass while in MyLauncher? Thank you.

makansij
  • 9,303
  • 37
  • 105
  • 183

1 Answers1

1

go up one directory then do

javac java_weirdness/*.java

and after

java java_weirdness.MyLauncher

form more info look here

PS: we're too much IDE dependent xD

Community
  • 1
  • 1
AlexGreg
  • 838
  • 10
  • 22
  • weird, so you have to compile the entire package? – makansij Sep 01 '15 at 07:56
  • try to remove the package declaration and it will work on your way – AlexGreg Sep 01 '15 at 07:58
  • 1
    Don't get too much into the habit of omitting the package declaration. For any serious software this is "bad style", in particular: classes in the "default package" (if a package declaration is missing) cannot be made visible to classes within a proper package. The Java Language Specification even allows different default packages which creates further confusion ... so, take it as convenience for smallest toy applications only. -- OTOH, even with packages you can compile classes one at a time, provided required classes have already been compiled, the point is to include the package name. – Stephan Herrmann Sep 05 '15 at 21:10