0

I know this is a basic question. I've been working with Java for a long time... However today I suddenly thought about creating a package without an IDE. So I thought the following should have worked :

package test;

class Node {

    int data;
    Node left, right;

    public Node(int data) {
        this.data = data;
        this.left = left;
        this.right = right;
    }
}

class SumBinaryTree {

    public static void main(String[] args) {
        ////
    }
}

Now, I thought that now package test contained class Node and class SumBinaryTree. After doing javac SumBinaryTree.java, I wrote the following code :

package test;

class Test {
    public static void main(String[] args) {
        Node t = new Node(0);
    }    
}

Now, since package test; is present, I thought this would work. However, on doing javac Test.java, I got an error with the two Node lines. I then realized that I did not know what was correctly going on and could use some help understanding this from you guys!

Thanks a lot.

gran_profaci
  • 8,087
  • 15
  • 66
  • 99
  • Have you tried using `javac SumBinaryTree.java Test.java`? – Chris Hayes Sep 03 '13 at 01:21
  • 3
    ...You have a very wrong / incomplete model of how Java compilation and running works. At a guess, I think the official tutorial on packages might be useful to you: http://docs.oracle.com/javase/tutorial/java/package/ Also relevant is the tutorial on the *class path* (http://docs.oracle.com/javase/tutorial/essential/environment/paths.html) and the technical note it links to (http://docs.oracle.com/javase/7/docs/technotes/tools/windows/classpath.html). – millimoose Sep 03 '13 at 01:22
  • This is how exactly programming was done in earlier days before IDE got in. Well try going out of the directory and then compile. `javac test/Test.java` and then run as `java test/Test`. – H-Patel Sep 03 '13 at 01:23
  • Look at the examples section in the javac documentation: http://docs.oracle.com/javase/7/docs/technotes/tools/windows/javac.html#examples – Eli Algranti Sep 03 '13 at 01:29
  • @H-Patel I'd dispute the historical claim. I'd say programming was done using some form of automated tooling for a very long time, insofar as it makes obvious sense for a programmer to be able to reproduce building a nontrivial program reliably. If it wasn't an IDE as we know it it was Emacs and a Makefile, or a bunch of shellscripts and utilities to manage things. – millimoose Sep 03 '13 at 01:31

2 Answers2

2

You need to include the package-

javac test/*.java
arshajii
  • 127,459
  • 24
  • 238
  • 287
Sajal Dutta
  • 18,272
  • 11
  • 52
  • 74
1

try this in your command line

    cd intoParentDirWhereNodeIs
    javac Node.java
    javac -d . Node.java

This should create a folder package called test in your files

shahidfoy
  • 1,951
  • 1
  • 17
  • 23