Results 1 to 4 of 4
  1. #1
    david is offline Member
    Join Date
    Aug 2016
    Posts
    2
    Rep Power
    0

    Default Program Compiling Successfully but getting error "Could not load or find main class"

    Java Code:
    C:\Users\pc\Documents>java
    Usage: java [-options] class [args...]
               (to execute a class)
       or  java [-options] -jar jarfile [args...]
               (to execute a jar file)
    where options include:
        -d32          use a 32-bit data model if available
        -d64          use a 64-bit data model if available
        -server       to select the "server" VM
                      The default VM is server.
    
        -cp <class search path of directories and zip/jar files>
        -classpath <class search path of directories and zip/jar files>
                      A ; separated list of directories, JAR archives,
                      and ZIP archives to search for class files.
        -D<name>=<value>
                      set a system property
        -verbose:[class|gc|jni]
                      enable verbose output
        -version      print product version and exit
        -version:<value>
                      Warning: this feature is deprecated and will be removed
                      in a future release.
                      require the specified version to run
        -showversion  print product version and continue
        -jre-restrict-search | -no-jre-restrict-search
                      Warning: this feature is deprecated and will be removed
                      in a future release.
                      include/exclude user private JREs in the version search
        -? -help      print this help message
        -X            print help on non-standard options
        -ea[:<packagename>...|:<classname>]
        -enableassertions[:<packagename>...|:<classname>]
                      enable assertions with specified granularity
        -da[:<packagename>...|:<classname>]
        -disableassertions[:<packagename>...|:<classname>]
                      disable assertions with specified granularity
        -esa | -enablesystemassertions
                      enable system assertions
        -dsa | -disablesystemassertions
                      disable system assertions
        -agentlib:<libname>[=<options>]
                      load native agent library <libname>, e.g. -agentlib:hprof
                      see also, -agentlib:jdwp=help and -agentlib:hprof=help
        -agentpath:<pathname>[=<options>]
                      load native agent library by full pathname
        -javaagent:<jarpath>[=<options>]
                      load Java programming language agent, see java.lang.instrument
        -splash:<imagepath>
                      show splash screen with specified image
    See http://www.oracle.com/technetwork/java/javase/documentation/index.html for more details.
    
    C:\Users\pc\Documents>javac
    Usage: javac <options> <source files>
    where possible options include:
      -g                         Generate all debugging info
      -g:none                    Generate no debugging info
      -g:{lines,vars,source}     Generate only some debugging info
      -nowarn                    Generate no warnings
      -verbose                   Output messages about what the compiler is doing
      -deprecation               Output source locations where deprecated APIs are used
      -classpath <path>          Specify where to find user class files and annotation processors
      -cp <path>                 Specify where to find user class files and annotation processors
      -sourcepath <path>         Specify where to find input source files
      -bootclasspath <path>      Override location of bootstrap class files
      -extdirs <dirs>            Override location of installed extensions
      -endorseddirs <dirs>       Override location of endorsed standards path
      -proc:{none,only}          Control whether annotation processing and/or compilation is done.
      -processor <class1>[,<class2>,<class3>...] Names of the annotation processors to run; bypasses default discovery process
      -processorpath <path>      Specify where to find annotation processors
      -parameters                Generate metadata for reflection on method parameters
      -d <directory>             Specify where to place generated class files
      -s <directory>             Specify where to place generated source files
      -h <directory>             Specify where to place generated native header files
      -implicit:{none,class}     Specify whether or not to generate class files for implicitly referenced files
      -encoding <encoding>       Specify character encoding used by source files
      -source <release>          Provide source compatibility with specified release
      -target <release>          Generate class files for specific VM version
      -profile <profile>         Check that API used is available in the specified profile
      -version                   Version information
      -help                      Print a synopsis of standard options
      -Akey[=value]              Options to pass to annotation processors
      -X                         Print a synopsis of nonstandard options
      -J<flag>                   Pass <flag> directly to the runtime system
      -Werror                    Terminate compilation if warnings occur
      @<filename>                Read options and filenames from file
    
    
    
    C:\Users\pc\Documents>javac prog.java
    
    C:\Users\pc\Documents>java prog
    Error: Could not find or load main class prog
    
    C:\Users\pc\Documents>

    Java Code:
    class prog{
    public static void main(String[] args){
    System.out.println("Hello World!");
    }
    }

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Program Compiling Successfully but getting error "Could not load or find main cla

    Nice post showing most of the details. It looks like it should execute and show the message.
    Is there a prog.class file in the C:\Users\pc\Documents folder?

    Is the classpath environment value set? Does it have a . in the path telling the java command to look in the current directory for class files?
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    david is offline Member
    Join Date
    Aug 2016
    Posts
    2
    Rep Power
    0

    Default Re: Program Compiling Successfully but getting error "Could not load or find main cla

    Quote Originally Posted by Norm View Post
    Nice post showing most of the details. It looks like it should execute and show the message.
    Is there a prog.class file in the C:\Users\pc\Documents folder?

    Is the classpath environment value set? Does it have a . in the path telling the java command to look in the current directory for class files?
    1.Yes a prog.class is being created in C:\Users\pc\Documents folder

    2.I am having some difficulty understanding the whole PATH & CLASSPATH thing.I am including a screenshot of my environment variables.Please tell what needs to be corrected.
    My JDK HOME is C:\Program Files\Java\jdk1.8.0_92


  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Program Compiling Successfully but getting error "Could not load or find main cla

    Many people recommend not using the classpath environment variable. Many programs require different sets of classes so it is better to provide access to those classes via the javac and java command's -cp option.

    what needs to be corrected.
    I recommend removing the classpath environment variable. Perhaps just rename it (add _SAVE to the name) for testing.

    PATH is used by the OS to find executable files like .exe and .bat files. It saves the user from having to enter the full path to an exe file. For example:
    "C:\Program Files\Java\jdk1.8.0_60\bin\javac.exe" -cp . -Xlint -Xdiags:verbose TestCode22.java
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Replies: 23
    Last Post: 12-12-2014, 09:24 AM
  2. Replies: 26
    Last Post: 08-01-2014, 03:56 PM
  3. Replies: 5
    Last Post: 06-12-2014, 03:12 AM
  4. Replies: 1
    Last Post: 11-06-2011, 12:26 AM
  5. Replies: 1
    Last Post: 06-30-2009, 02:32 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
  •