A pain in the ......Please help?

Dear Ranchers, I always have problems with setting the class path variable. Setting it has no effect in the current context. I enter this at the root: C>set classpath = c:\jdk1.3\lib;c:\classes\; Has no effect. My objective is to run the program (includes a group of class files in the same package) from any directory. Here are the steps I followed. Tell me what Im doing wrong. All my source files are kept in the following top level directory: c:\sources so the full path is c:\sources\com\mydomain\.... All of my class files are kept in the following top level directory: c:\classes so the full path is c:\classes\com\mydomain\.... Two simple files are created with the same name package for the purpose of this demonstration. ---------------------------------------------------------- //Package.java package com.mydomain.Test; class Package{ Package(){ System.out.println("Im in the Package constructor"); } } ----------------------------------------------------------- //TestPackage.java package com.mydomain.Test; class TestPackage{ public static void main(String[] args){ Package p = new Package(); } } ----------------------------------------------------------- Package.java is compiled first. The generated class file is placed into the Test folder beneath the c:\classes structure TestPackage.java doesnt compile unless I use the -classpath option. ie. javac -classpath c:\classes\; TestPackage.java After compiling this I place its class file into the same folder beneath the c:\classes structure Now the two classes are in the same package. Next I attempt to run TestPackage from the C root: Correct way C>java com.mydomain.Test.TestPackage and it doesnt run. but if I use the classpath option: C>java -cp :\classes\; com.mydomain.Test.TestPackage No problem! What am I doing wrong ranchers? Or is there another way to set the classpath variable to the context? Please help me on this one ranchers because I need to move on.

[cut] C:\java com.mydomain.Test.TestPackage and it doesnt run. but if I use the classpath option: C:\java -cp :\classes\; com.mydomain.Test.TestPackage No problem! What am I doing wrong ranchers? [cut]

Everything is fine. The second command is correct. And, honestly, I don't understand why are You so surprised. How shoud java know (in the first case) where Your classes are? It'd have to search all folders in C:\ to find Your com.mydomain.Test package. You have to tell where it is, that means in classpath You have to give the folder name where top of the package folder structure is. If You want to run Your program from anywere, just create a batch file in C:\classes containing: java -classpath . com.mydomain.Test.TestPackage [ February 12, 2002: Message edited by: Maciej Kolodziej ]

MK

Stephen Batsas
Ranch Hand

Joined: Jan 22, 2002
Posts: 117

posted Feb 12, 2002 05:26:00

0

Thanks for the info. The problem: Creating a batch file defeats the purpose of being able to run the package from any directory. I would have to place copy of the batch file in every directory.

No Stephen, just put the .bat file in a directory that is in the path and it will be found from whatever directory you are sitting in when you call it. If you want to have the .bat file in a specific directory, then just put that directory into the path.