Results 1 to 4 of 4
  1. #1
    kulangotski is offline Senior Member
    Join Date
    Feb 2009
    Posts
    117
    Rep Power
    0

    Default cannot run the class file using the java command

    hi. I am having trouble running simple codes due to some problems.

    I have my class here name MyClass.java inside a package

    Java Code:
    package com.sm;
    class MyClass {
    	public static void main (String[] args) {
    		System.out.println("FUBAR");	
    	}
    }
    and I have some folders structured like this

    MyProject> source,classes

    source>com>sm>MyClass.java
    classes>com>sm>MyClass.class

    I was able compile the application and put it in the classes>com>sm folder using this code

    Java Code:
    javac -d classes (diretoryToMyJavaFile)/MyClass.java
    ok that is all fine, but the problem is I cannot run the application using the java command. what i do is I go to the folder where my class file is and then try to do "java MyClass" but it seems like it is not accepting the file it gives me

    Java Code:
    Exception in thread "main" java.lang.NoClassDefFoundError: MyClass (wrong name: com/sm/MyClass)
    what I did recompiled the simple file without the package declaration up top and it worked. seems like it does not want to be included in the package I put it into. are there any problems with the package name? or are there certain rules that I need to follow so I can give my java file a package declaration?

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    What if you try to run it from the classes directory via

    Java Code:
    java com.sm.MyClass

  3. #3
    kulangotski is offline Senior Member
    Join Date
    Feb 2009
    Posts
    117
    Rep Power
    0

    Default

    that did it! thanks! :DI guess need to read more about packages :)
    anyway thanks again

    regards,
    simon

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

Similar Threads

  1. cannot find the class file when running the java command
    By kulangotski in forum New To Java
    Replies: 4
    Last Post: 01-18-2011, 12:34 PM
  2. converting java class file to exe file
    By satheeshtech in forum Advanced Java
    Replies: 5
    Last Post: 07-19-2009, 12:47 AM
  3. Replies: 2
    Last Post: 12-03-2008, 02:24 PM
  4. Filter a file in java (unix cut command equivalent)
    By marcosabel in forum New To Java
    Replies: 0
    Last Post: 02-11-2008, 08:26 PM
  5. Unable to execute command line command in java
    By LordSM in forum New To Java
    Replies: 1
    Last Post: 08-08-2007, 01:23 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •