Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

java interpretor and CLASSPATH?  RSS feed

 
Pourang Emami
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Environment Valuables:
OS: WindowsXP
CLASSPATH = .;C:\Documents and Settings\Pourang Emami;
PATH = C:\j2sdk1.4.2\bin
JAVA_HOME = C:\j2sdk1.4.2
Problem:
When I try to comiple some java file like Cls1.java outside the path indicated in CLASSPATH, it will be compiled but wont get interpreted(although the class file is generated by the compiler):
C:\>javac C:\OtherFolder\Cls1.java
C:\>java C:\OtherFolder\Cls1
Exception in thread "main" java.lang.NoClassDefFoundError: C:\OtherFolder\Cls1
Thank you for your guidance.
 
Chandra Bairi
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do one thing. Put the java in the path variable and it should work properly.
You have java installed in some directory let us say
C:\java.
you can add the following to the path as follows
set path=%path%;c:\java\bin
and then try to execute the file. It should work properly.
Regards,
Shekar.
 
Chandra Bairi
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i am sorry i have not read the question completely. You add the the directory
i.e
C:\otherFolder to the class path and then try to execute the java statement.
You will surely execute the file.
Shekar.
 
Chandra Bairi
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I forgot to tell other thing Now after adding it in the classpath
execute java Cls1
shekar.
 
Pourang Emami
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Chandar
So we should always add the folder containing our java files to CLASSPATH, and interprete them inside this folder?
 
Manoj Zachariah
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
or you can run the command

where c:\dir is the path of your java program - file.java
 
Manoj Zachariah
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry mistake while typing

 
Chandra Bairi
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes My friend(I forgot your name)
We should always add the directory in which the classes are there to the classpath.
shekar.
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Note that it's pretty easy to do this by simply including the current working directory in the CLASSPATH setting (specified by a period) and then working from the directory that contains the class (or that contains the base package of the class (so to speak)).
 
Marilyn de Queiroz
Sheriff
Posts: 9080
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Pourang Emami:
So we should always add the folder containing our java files to CLASSPATH, and interprete them inside this folder?

You are almost correct. We should always add the folder containing our class files (after the java files are compiled to class files) to our CLASSPATH. If they are in the classpath, then you do not need to interpret them inside that folder.

However, as Dirk said, the easiest way is to work from the folder where your source files are. For example, C:\java.
cd (change directory) to the C:\java folder.
Compile javac Test.java while you are in the C:\java folder.
Run Test (Test.class is in C:\java) while you are still in C:\java.

(CLASSPATH=.)
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!