• Post Reply Bookmark Topic Watch Topic
  • New Topic

Can java class file not work without setting classpath ?  RSS feed

 
nirjari patel
Ranch Hand
Posts: 386
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a java source file in a path, when I compile this source file by typing path to this file and also path to javacon cmd prompt , it compile and classfile is generated in same location where source file is.
Now when I am trying to execute classfile by typing path to class file and also path to java exe file on cmd prompt , I am getting main not found error as below
C:\Users\um2t\Desktop\Java>"C:\Program Files\java\jdk1.5.0_22\bin\javac" "C:\Users\um2t\Desktop\Java\TestPath.java"

C:\Users\um2t\Desktop\Java>"C:\Program Files\java\jdk1.5.0_22\jre\bin\java" "C:\Users\um2t\Desktop\Java\TestPath"
Exception in thread "main" java.lang.NoClassDefFoundError: C:\Users\um2t\Desktop\Java\TestPath

DO I have to have classpath setup for class file to be execured ? Can a classfile not run by typing path on cmd prompt ? If so, why ?
 
Paul Clapham
Sheriff
Posts: 22829
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sure you can type the classpath for the class as part of the cmd prompt. The syntax is like this:

java -cp classpath class


Have a look at this tutorial for a better description than what I could provide. Notice also that setting the PATH environment variable allows you to just type "java" instead of "C:\Program Files\java\jdk1.5.0_22\jre\bin\java" as you did, so read that part of the tutorial carefully as well.

(And why are you using Java 5? It has been obsolete and unsupported for years. Unless you have some reason for using Java 5 -- like somebody else is making you do that -- you should be using Java 7 now.)

 
nirjari patel
Ranch Hand
Posts: 386
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks. But I am still getting error when its executed as follows
C:\Users\um2t\Desktop>"C:\Program Files\java\jdk1.5.0_22\jre\bin\java" -cp classpath "C:\Users\um2t\Desktop\Java\TestPath"
Exception in thread "main" java.lang.NoClassDefFoundError: C:\Users\um2t\Desktop\Java\TestPath


C:\Users\um2t\Desktop>"C:\Program Files\java\jdk1.5.0_22\jre\bin\java" -cp "C:\Users\um2t\Desktop\Java\TestPath"
Usage: java [-options] class [args...]
(to execute a class)
or java [-options] -jar jarfile [args...]
(to execute a jar file)

where options include:
-client to select the "client" VM
-server to select the "server" VM
-hotspot is a synonym for the "client" VM [deprecated]
The default VM is client.

-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>
require the specified version to run
-showversion print product version and continue
-jre-restrict-search | -jre-no-restrict-search
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
-da[:<packagename>...|:<classname>]
-disableassertions[:<packagename>...|:<classname>]
disable assertions
-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
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
nirjari patel wrote:Thanks. But I am still getting error when its executed as follows
C:\Users\um2t\Desktop>"C:\Program Files\java\jdk1.5.0_22\jre\bin\java" -cp classpath "C:\Users\um2t\Desktop\Java\TestPath"
Exception in thread "main" java.lang.NoClassDefFoundError: C:\Users\um2t\Desktop\Java\TestPath



Well, in your example, you declared a classpath as "classpath". Do you have your classes in a directory named "classpath"?

Henry
 
nirjari patel
Ranch Hand
Posts: 386
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I got it. Thanks, its working now
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
nirjari patel wrote:When I execute as follows, I still get same error
C:\Users\um2t\Desktop>"C:\Program Files\java\jdk1.5.0_22\jre\bin\java" -cp "C:\Users\um2t\Desktop\Java\" TestPath
Usage: java [-options] class [args...]
(to execute a class)
or java [-options] -jar jarfile [args...]
(to execute a jar file)


That is a usage error -- which is completely different from a class not found error. You get this message when the JVM has no idea what you are trying to say, and is giving you a usage message with the correct parameters.

Are you sure you are cut and pasting what you are typing? You shouldn't be getting that message based on what you showed.

Henry
 
nirjari patel
Ranch Hand
Posts: 386
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I got it. Thanks, its working now
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!