• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

java.lang.ClassNotFoundException

 
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All.

I'm using netbeans and get the following output:

run:
java.lang.NoClassDefFoundError: battleships/BattleShips
Caused by: java.lang.ClassNotFoundException: battleships.BattleShips
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
Could not find the main class: battleships.BattleShips. Program will exit.
Exception in thread "main" Java Result: 1
BUILD SUCCESSFUL (total time: 0 seconds)

My main class is named after the file i'm using, i've cut and pasted from source code into NetBeans

If I click on properties > run for the project it can't find the main class. Furthermore the class dir of the project is empty.

Any ideas?

Thanks,
Rob.
 
Ranch Hand
Posts: 300
Eclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

1) Try building your project and then check if its able to generate class files.

2) See if there is unresolved reference for your project for any library or jar etc in that case your project will not build successfully and give error.
 
Rob Brew
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The classes get compiled ok, the compiler runs alright, but netbeans can't find the main thread, even though it's names in the same class as the file.

Completely stuck.
 
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Does your Battleships contain a main method? If yes can you copy paste the method signature for us?
 
Rob Brew
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Tries running the class from the command line, still no joy. When i try to run the project from netbeans a dialouge box comes up saying "No main classes found".
 
Ranch Hand
Posts: 710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've never worked with Netbeans, but it looks like its looking for class BattleShips, but your class name is battleships.
 
Rob Brew
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I refactored (changed) the class name to battleships to see if that would make a difference, still no joy.
 
Sheriff
Posts: 3837
66
Netbeans IDE Oracle Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rob Brew wrote:I refactored (changed) the class name to battleships to see if that would make a difference, still no joy.


Just to clarify, your own post from 18:20 indicates the class name is battleships (all lowercase). That might be the problem.

Anyway, you can try to verify the project settings: right-click the project in the Projects windows and choose Properties. Select the Run category in the leftmost pane. What do you see in the Main Class textfield? Can you click the associated Browse button and select the correct main class?
 
Rob Brew
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The class name hase been changed to battlehsips all lower case. I've tried the properties > run and selected the path but no main class can be found. The program compiles and runs in java fine. just not here.
 
Martin Vashko
Sheriff
Posts: 3837
66
Netbeans IDE Oracle Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rob Brew wrote:I've tried the properties > run and selected the path but no main class can be found.


Do you refer to the runtime exception, or to the Properties form? In other words, can you select the battleships class in the Main class field of the Properties form? (If not, that is certainly part of the problem.)

What package is your class in? Does the directory structure of your project reflect the package declarations in your class(es)?
 
Rob Brew
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
when i hit properties > run i get a <No main classes found> error message. It's set to battleships.battleships. When i try to run the program that error message appears again. I've tried putting the build directory on the run tab of the application, still no joy.

Each file is in the battlehsips package.

The runtime classpath of the main class battlehsips is set correctly. I can't set battleships as the main class in the properties form, even with the directory typed in.

Using java on the command line fails with the package declaration at the start of each file. Without this java from the comman line can run the program. I know i need to set the run paramater on the properties box, but what to?
 
Martin Vashko
Sheriff
Posts: 3837
66
Netbeans IDE Oracle Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First of all, you sometimes state the class or package name as battlehsips instead of battleships - I assume that it is a typo, if not, it might be the problem itself. Double check the typo is not in your class, package or directory names.

Do you have your source file (battleships.java) located in the src/battleships directory? This is the default organization of Netbeans projects; Netbeans could have problems locating your classes if this is not the case.

What OS do you use (Windows or Unix)? Under most circumstances, Unix, unlike Windows, is sensitive to case in file/directory names.
 
Rob Brew
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Martin.

I'm on windows and yes files are in the src/battleships directory. I've editied my last post so take a look.
 
Martin Vashko
Sheriff
Posts: 3837
66
Netbeans IDE Oracle Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you run your project from NetBeans at least by choosing Run/Run File command while editing your main class?

Rob Brew wrote:Using java on the command line fails with the package declaration at the start of each file. Without this java from the comman line can run the program.


Do you mean that if you remove the package declaration from source files, you can run the application from command line (using java), but if you keep the package declaration, it does not work? In this case, I think the classpath parameter should help.

I'd advice to recreate the project anew from start, so that NetBeans creates the directory structure as it likes (make sure to create packages and classes by using NetBeans' commands and keep package declarations there), and copy at least parts of the sources there via clipboard. If it works this way, you can then compare the created files and directories and possibly spot the difference.
 
Rob Brew
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Pasted everything in again without redefining the main class. Works now thank you
 
rubbery bacon. crispy tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic