• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

Problems running with package name used

 
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.



 
Marshal
Posts: 76802
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can find about naming packages in the Java Tutorials.
 
What's wrong? Where are you going? Stop! Read this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic