javac and java -classpath concepts

1)Is there a way to set the classpath variable in both javac and java so that I do not need to convey it explicitly in the command line via navigating to the directory or as an arguement? For example, in Control Panel/System/Advanced/Environment Variables--User or System)

2)Is it true:  You only need to specify the entire package name after it has been compiled. (When running it) One only needs to provide the name of the file.java when compiling if the are in the corresponding directory.


kellyputtyAsked:
Who is Participating?
 
mrigankCommented:
Use

cmd>set CLASSPATH=%CLASSPAHT%;C:\java\code
0
 
hoomanvCommented:
1) yes
set this environment variable
set CLASSPATH=you classpath
0
 
hoomanvCommented:
may be helpful
mindprod.com/jgloss/classpath.html
0
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

 
kellyputtyAuthor Commented:
The following is what I have set but javac and java don't "see" it:
CLASSPATH=C:\java\code

What could I be doing incorrectly such that javac and java aren't "seeing" the environment variable CLASSPATH?
0
 
hoomanvCommented:
type the command you use to run the class
0
 
mrigankCommented:
1) Yes.

a)My Computer->Proprties->Advanced->Environment Variables

Add a system variable called CLASSPATH. Put the directories that you want there as the value of the variable.

b) As hoomanv said, set CLASSPATH=%CLASSPATH%;Your Classpath here.

2) Yes it is true.
0
 
mrigankCommented:
2) If you have a class a.b.c.D.java

javac is like

home\a\b\c>javac D.java

java is like

home>java a.b.c.D
0
 
mrigankCommented:
sorry, a typo

cmd>set CLASSPATH=%CLASSPATH%;C:\java\code
0
 
kellyputtyAuthor Commented:
mrigank said:
My Computer->Proprties->Advanced->Environment Variables

Add a system variable called CLASSPATH

>>CLASSPATH=C:\java\code;C:\java\oreilly\examples;  (I have source code in both)
-----------------
hoomany asked:
 type the command you use to run the class
>>
C:\java\code\c08\music5>javac Music5.java
But even the the CLASSPATH system variable is set, javac doesn't "see" it.
0
 
kellyputtyAuthor Commented:
mrigank, thank you that worked---
cmd>set CLASSPATH=%CLASSPATH%;C:\java\code
0
 
mrigankCommented:
:-)

no worries.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.