• Post Reply Bookmark Topic Watch Topic
  • New Topic

Adding package name to sourcecode causes problem when compiling/running from command line?  RSS feed

 
John Mulholland
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am operating java from the command line (using Terminal on Mac OSX 10.9.5).
The current directory within Terminal is called "orange" and the following sourcecode file is in the orange directory :

Test1.java


This complies to Test1.class (visible in the orange directory) but when I try to run it in Terminal (simply using "java Test1" from within the orange directory)
I get the following error message (which I don't get if I simply comment out the line //package orange;)

Would really appreciate any help you guys might be able to offer to a Mac Terminal user like me ?

Exception in thread "main" java.lang.NoClassDefFoundError: Test1 (wrong name: orange/Test1)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:800)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:482)
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John Mulholland wrote: when I try to run it in Terminal (simply using "java Test1" from within the orange directory)


The "java Test1" command is looking for a Test1 class that is not in a package. If the Test1 class is in the orange package, you need to run it via "java orange.Test1". And you also need to have the root in the classpath, meaning either running it from the parent directory of the orange directory, or have the parent directory in the classpath.

Henry

 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

As Henry has hinted, the class is not called Test1 but orange.Test1 and is most easily accessed from where you can see the orange directory. Try this
And I added code tags to your post. Always use the tags: doesn't it look better
 
John Mulholland
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks guys ... that worked.

I noticed a similar problem with package names and javac. If I compile these two short files below (both in the
orange directory) using javac *.java, they both compile fine and run fine (using the method you showed me :-)).

But if I try to compile using javac Test1.java (which normally auto-compiles the referenced Test2.java - as
both files are in the same directory), it wont compile UNLESS I remove the package names from both sourcecode files ???

It seems that the compiler can't see Test2.java automatically when Test2.java and Test1.java are named in a specific package
but it CAN see Test2.java and auto-compile it if I use the default package ? It would really help my understanding if you could explain that


Test1.java

Test2.java


Attempt to compile (using Terminal in the current orange directory) :

javac *.java (works fine)

javac Test1.java gives the following error :

Test1.java:6: error: cannot find symbol
Test2 myTest = new Test2();
^
symbol: class Test2
location: class Test1
Test1.java:6: error: cannot find symbol
Test2 myTest = new Test2();
^
symbol: class Test2
location: class Test1
2 errors
 
Tim Holloway
Saloon Keeper
Posts: 18800
74
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By convention, not only the names and contents of Java files matter, their locations matter as well.

Thus, if you have a class whose fully-qualified name is "com.coderanch.forums.ForumList", the proper location for the source file would be something like src/com/coderanch/forums/ForumList.java and the compiled output would go to classes/com/coderanch/forums/ForumList.class (using option "-o classes" on javac)

Import statements provide a shorthand, but the real name of a class includes its package path.

Building a set of classes from the command line can be a bit annoying, which is why serious projects use Ant or Maven to do the compiling and assembling for them. I used Windows batch files long ago, but that wasn't very pleasant.
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
~/java$ mkdir orange
~/java$ cd orange/
~/java/orange$ gedit Test1.java Test2.java
~/java/orange$ javac Test1.java
Test1.java:6: error: cannot find symbol
‍ . . .
~/java/orange$ javac Test2.java
~/java/orange$ javac Test1.java
Test1.java:6: error: cannot find symbol
‍ . . .
~/java/orange$ cd ..
~/java$ javac orange/Test1.java
~/java$ java orange.Test1
Test1 works
Test2 works
As you can see, the javac tool wants to compile from the root of the directory structure. You will also find that code in other packages ought to be compiled first so dependencies in other packages are fulfilled. If you remove the .class files from orange, you can compile both classes in the same package like this
java$ ls orange/*
orange/Test1.java orange/Test2.java
~/java$ javac orange/Test1.java
~/java$ java orange.Test1
Test1 works
Test2 works
Note you use orange/ for compiling and orange. for the java tool.
 
John Mulholland
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the help guys ... :-)
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!