File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes java.lang.ClassNotFoundException Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "java.lang.ClassNotFoundException" Watch "java.lang.ClassNotFoundException" New topic
Author

java.lang.ClassNotFoundException

Rob Brew
Ranch Hand

Joined: Jun 23, 2011
Posts: 100
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.
Javin Paul
Ranch Hand

Joined: Oct 15, 2010
Posts: 294


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.


SQL Query Interview Question
Rob Brew
Ranch Hand

Joined: Jun 23, 2011
Posts: 100
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.
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10535
    
    9

Does your Battleships contain a main method? If yes can you copy paste the method signature for us?


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Rob Brew
Ranch Hand

Joined: Jun 23, 2011
Posts: 100


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".
W. Joe Smith
Ranch Hand

Joined: Feb 10, 2009
Posts: 710
I've never worked with Netbeans, but it looks like its looking for class BattleShips, but your class name is battleships.


SCJA
When I die, I want people to look at me and say "Yeah, he might have been crazy, but that was one zarkin frood that knew where his towel was."
Rob Brew
Ranch Hand

Joined: Jun 23, 2011
Posts: 100
I refactored (changed) the class name to battleships to see if that would make a difference, still no joy.
Martin Vajsar
Sheriff

Joined: Aug 22, 2010
Posts: 3611
    
  60

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

Joined: Jun 23, 2011
Posts: 100
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 Vajsar
Sheriff

Joined: Aug 22, 2010
Posts: 3611
    
  60

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

Joined: Jun 23, 2011
Posts: 100
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 Vajsar
Sheriff

Joined: Aug 22, 2010
Posts: 3611
    
  60

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

Joined: Jun 23, 2011
Posts: 100
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 Vajsar
Sheriff

Joined: Aug 22, 2010
Posts: 3611
    
  60

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

Joined: Jun 23, 2011
Posts: 100
Pasted everything in again without redefining the main class. Works now thank you
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: java.lang.ClassNotFoundException