This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Problems running with package name used  RSS feed

 
Angus Comber
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a Java program like so:



This is from a book and so should be fine.. I am playing with packages so I store this java file in this folder:
\src\com\example\web\

At this level I compile like this:
javac -d classes src\com\example\web\test_props.java

It appears to compile without any errors

But when I go to run from this same level I get:

java classes\com\example\web\test_props
Exception in thread "main" java.lang.NoClassDefFoundError: classes\com\example\web\test_props (wrong name: com/example/web/test_props)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:791)
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:423)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:476)


Or
java .\classes\com\example\web\test_props

Error: Could not find or load main class .\classes\com\example\web\test_props

Even if I navigate to \classes\com\example\web\test_props and then try java test_props I get:
Error: Could not find or load main class test_nw_client

??? What the?

If I remove the package com.example.web; line in source and compile with javac test_props.java in this same folder as file and then java test_props everything works nicely - ie prints out loads of info.

What am I doing wrong?
 
Angus Comber
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Angus Comber wrote:I have a Java program like so:



This is from a book and so should be fine.. I am playing with packages so I store this java file in this folder:
\src\com\example\web\

At this level I compile like this:
javac -d classes src\com\example\web\test_props.java

It appears to compile without any errors

But when I go to run from this same level I get:

java classes\com\example\web\test_props
Exception in thread "main" java.lang.NoClassDefFoundError: classes\com\example\web\test_props (wrong name: com/example/web/test_props)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:791)
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:423)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:476)


Or
java .\classes\com\example\web\test_props

Error: Could not find or load main class .\classes\com\example\web\test_props

Even if I navigate to \classes\com\example\web\test_props and then try java test_props I get:
Error: Could not find or load main class test_nw_client

??? What the?

If I remove the package com.example.web; line in source and compile with javac test_props.java in this same folder as file and then java test_props everything works nicely - ie prints out loads of info.

What am I doing wrong?


Answering my own question...

I have read the manual now (Well actually another post).

Ahhhhh, I have to run like this:
cd classes
java com.example.web.test_props

 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have to provide the fully qualified class name, using periods for separators, and one element of your classpath must be the parent directory of the package root.



 
Campbell Ritchie
Marshal
Posts: 55751
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can find about naming packages in the Java Tutorials.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!