Results 1 to 8 of 8
  1. #1
    miaaa00 is offline Member
    Join Date
    Mar 2011
    Posts
    9
    Rep Power
    0

    Default My first java program could not be executed..help

    Hello,

    import java.io.*;

    public class firstjava1
    {
    public static void main(String args[])
    {
    System.out.println("hi");
    }
    }

    C:\>javac firstjava1.java

    C:\>java firstjava1
    Exception in thread "main" java.lang.NoClassDefFoundError: firstjava1
    Caused by: java.lang.ClassNotFoundException: firstjava1
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClassInternal(Unknown Source)
    Could not find the main class: firstjava1. Program will exit.

    Thanks in advance..

    I have coded the above java program and it creates the java class file when compiled but it don't allows me to execute the file..

    My Path is set as : C:\Java\jdk1.6.0_24\bin

    Can anyone help me out where's the mistake...??

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    go to the directory it's in and compile it with the classpath flag.

    Java Code:
    java -cp . firstjava1
    Also, when creating classes you want to follow correct practice(capitalize first letter of each word
    Java Code:
    public class FirstJava1
    Also, it may be helpful to save your java code to a new directory, for instance I tend to have an easier time having a directory located at c:\javacode, and in this directory I store all my java code, and set the path to point to this directory.

    Also, consider adding . to your classpath, this will allow it to use your current directory so you can just cd to the directory and compile and run easily.

    Let me know if this works, if not, I will try my best to help you out more.

  3. #3
    miaaa00 is offline Member
    Join Date
    Mar 2011
    Posts
    9
    Rep Power
    0

    Default

    Hello,

    thanks for your reply and help..

    i tried out as you suggested.

    from your suggestions i tried all in the different ways as you mentioned..

    It could give the output only by using -cp
    java -cp . FirstJava1

    If i tried it
    java FirstJava1
    it gives me the error why is it so ??

    For what -cp is used to execute the program ??

  4. #4
    miaaa00 is offline Member
    Join Date
    Mar 2011
    Posts
    9
    Rep Power
    0

    Default

    thanks....

    so i have to change the path that means...anyways
    if i want to run in simple java filename then i can't execute the file or what ??

    i have to set my path within C:\program files\java only?? please tell me the path to execute the file..Thanks for your help..It cleared my questions now just help me to set the path..
    Last edited by miaaa00; 03-20-2011 at 03:27 PM.

  5. #5
    akiravelmont is offline Member
    Join Date
    Mar 2011
    Location
    bangalore
    Posts
    61
    Rep Power
    0

    Smile

    to set the path
    right click on MyComputer->choose properites->choose tab Environmental variables-> go to system variables section check for PATH variable is there. if available select that one and click edit
    in the value section path is already set for other ones so go to the end of the value like c:/p files/.../...;c:/prog files/..../....;.;
    just go end remove ;.; and paste the java path without effecting the previous like this:
    c:/p files/.../...;c:/prog files/..../....;c:/program files/java/jdk1.6/bin;.;
    then click ok button.

    we can differentiate the path by ; (semicolon), this is the delimiter for several paths

  6. #6
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    As stated above, using ;(semicolons) lets you add multiple classpaths. For example, it may be useful to have your classpath set up with the bin(like you already have), followed by these two commands
    Java Code:
    theBinFolder;c:\javacode;.
    This will allow you to compile and run files in the a directory called javacode, and the current directory.

    The reason -cp . works is because it's a special flag which tells the jvm to use the following classpath when executing the program. The command . means current directory, so using this flag with this makes it work.

    If you have a file in the directory
    Java Code:
    c:\javacode
    You can also compile and run with
    Java Code:
    javac -cp c:\javacode FileName.java
    
    java -cp c:\javacode FileName
    Here is more reading if you are up to it
    java - the Java application launcher
    javac - Java programming language compiler

    Finally, the suggestion on using
    Java Code:
    FirstJava1
    is more of a do this, not that.

    When you write programs you should make your file and class name have a capitalized first letter(this is not needed, but generally accepted)

  7. #7
    akiravelmont is offline Member
    Join Date
    Mar 2011
    Location
    bangalore
    Posts
    61
    Rep Power
    0

    Default

    you said that to run java -cp . filename to execute.


    if we set the like theBinFolder;c:\javacode;. can we directly execute the program like this?

    c:\javacode> java FirstJava

    or else can we place the -cp . for every time we run

  8. #8
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

Similar Threads

  1. instruction apparently not executed
    By rippon in forum AWT / Swing
    Replies: 4
    Last Post: 11-30-2010, 01:30 AM
  2. Finally does not get executed
    By rahulkumar in forum New To Java
    Replies: 8
    Last Post: 04-06-2010, 05:30 PM
  3. Replies: 9
    Last Post: 11-01-2009, 05:45 AM
  4. Will finally get executed...
    By AlmostAGuru in forum New To Java
    Replies: 1
    Last Post: 08-10-2009, 07:12 PM
  5. Replies: 0
    Last Post: 04-04-2008, 02:40 PM

Posting Permissions

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