• 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
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

ClassNotFoundException with class in same package

 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm building an application using JDK 1.6, Eclipse, Spring, and Maven. I don't think any of these play a role, but wanted to give the background. I have a Java class, MMThread, that extends Thread. I needed an extra class - very basic data object - only used in the MMThread class. So I decided to make this data class, User, a private top level class.

All builds fine, but when I get to the run method in the MMThread code, my 'new User(name, loc)' causes a ClassNotFoundException. So I decided to extract the private User class, make it public and put it in its own file in the same package as MMThread. As expected all builds well, but I still get a ClassNotFoundException. I've done a 'Clean', a Maven Clean, a Maven Update Project, rebuild, and attempted to run it again. But I'm still getting the exception.

I've looked at the generated JAR file and the class file is there - and there's only one - so I'm still trying to figure it out.

All help appreciated.

Thanks.

Les
 
Ranch Hand
Posts: 924
1
Netbeans IDE Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
first of all you cannot declare your top level class private or protected. the only access modifier it can have is either public or package-private(default).

since your both classes are in same package , i think you are running the class from wrong directory. consider an example . suppose i have 2 classes A and B in com.guru.ranch directory.

c://test/com/guru/ranch/A.java and c://test/com/guru/ranch/B.java

now A class USES class B. now how will you run them(assume they are compiled)

to run them you should be in test directory and type java A(in other words your test directory must be in classpath)

did you understood it ? put it in a generalized way, PARENT OF THE PACKAGE ROOT SHOULD BE IN CLASSPATH.
 
Les Hartzman
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I understand what you are saying. But you didn't keep reading my message:

So I decided to extract the private User class, make it public and put it in its own file in the same package as MMThread

I took the private class out of the file and made it public in its own file.

There is another class in the same package that starts up the threads. So this other class is actually class 'A', which uses class 'B', which uses class 'C'. Since 'A' can see and use 'B', why is 'C' not being seen? Obviously the directory hold A, B, and C is in the classpath or none would be found and executed.

Les
 
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
It will be easier to diagnose what's wrong if you show the directory layout of your .class files, your classpath, the command you're using to execute your app, and the package and import statements of the relevant classes.
 
Les Hartzman
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is strange, but as I was getting ready to put together a message laying out the directory structure and other details, I rebuilt one more time and this time it worked! I don't know if Eclipse was in some state or what, but it started to work. It should have worked once I made the private class public and put it into its own file (all the .class files were in the same directory), but it did not. Then suddenly poof!

Les
 
Les Hartzman
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well it reared it ugly head again. I brought in another co-worker more experienced with Eclipse than I am, and he was at a loss also. It turns out that the code in question was calling a method and one of the parameters was an object of the class that couldn't be found. The method was called with 'new NotFoundClass(a, b)'. When the object creation was taken out of the method call, the exception stopped happening.

It didn't matter if I had the class as a public class in its own file or if I made it an inner class where the method was called.

If I get a chance I'll try to duplicate this outside of the application (something simpler to deal with). I'll also try Java 7 in case there is a bug in Java 6 (update 29).

Les
 
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's rather hard to comment without seeing the code, the exception message and it's stack trace and all the stuff Jeff asked for that you haven't yet provided.
 
I suggest huckleberry pie. But the only thing on the gluten free menu is this tiny ad:
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic