• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Setting the classpath

 
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Hi,

I'm attempting to set the classpath environment variable to compile and execute programs from the command line but unsuccessful so far! I've tried the following:



At this stage the preceding error is displayed. What am i doing wrong?

Could any of the forum experts / members help out.

Other info:
O.S - Windows XP (SP 2)

thanks,
Sudhir

P.S:

Alternately, i tried the following way of compiling and running a program

Steps:
--------
  • copy pasted the path leading to the bin directory(of the installation directory) to the command window
  • copy pasted the source file (.java) from the source to the bin directory
  • compiled the program.......bin directory then generates the corresponding .class file
  • executed the program


  • and it works!!



    which i don't think is the right way, as I'm doing manually - for every program - what should be done by the O.S globally, through setting the classpath, for all programs.




     
    Sheriff
    Posts: 22781
    131
    Eclipse IDE Spring VI Editor Chrome Java Windows
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Sudhir Srinivasan wrote:which i don't think is the right way, as I'm doing manually - for every program - what should be done by the O.S globally, through setting the classpath, for all programs.


    You're right and you're wrong. No, this isn't the right way. However, it isn't the CLASSPATH variable you need to set but the PATH variable.

    This FAQ entry explains how to set the CLASSPATH variable globally. The same instructions work for the PATH variable, with one caveat - you should not remove the existing PATH contents, but add to them (using ; to separate paths). By adding the JDK folder to the start you will make sure that no other JDK (or JRE) will be preferred.
     
    Ranch Hand
    Posts: 143
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    No argument with the solution proposed as far as it working. The only thing I will add is to be careful when modifying your path.

    In this case, you would appear to not have any conflicts (i.e. other JDKs installed) or it would have worked for you no matter what directory it was run from, so this caution is more as an FYI as your java experience grows.

    Some software which you may elect to install in the future may be bundled with its own JDK or JRE and sometimes such bundles insert themselves at the beginning of the path as well which override entries later in the path. Depending on the versions that are installed and how they are ordered you can get some odd errors.

    Some options which can help you avoid this include setting up a directory of batch files in your path that you can use to set up the path for the command window you have opened. The batch files will add the jdk to the path for your command window session only - when closed they go away. For example you could have batch files for "JDK6", "JDK7", "IBMJDK", etc depending on with what JDK you want to work.
     
    Marshal
    Posts: 79151
    377
    • Likes 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Avoid putting any of your own work into the Program Files folders.
     
    Sudhir Srinivasan
    Ranch Hand
    Posts: 93
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator


    Hi,

    Thank you Rob, Brian and Campbell for your responses.


    Rob - The environment variables viz. My Computer > Properties > Advanced tab > Environment variables > System variables does contain the CLASSPATH with its value set to .;C:\Program Files\Java\jre6\lib\ext\QTJava.zip. As per the FAQ link provided, this would be a default setting at the time of installation of the JDK with the built-in class libraries.

    Since it is not the CLASSPATH variable

    Rob Spoor wrote:However, it isn't the CLASSPATH variable you need to set but the PATH variable.



    but the PATH variable, I looked at Environment variables > System variables again and found the PATH variable having existing values

    Rob Spoor wrote:This FAQ entry explains how to set the CLASSPATH variable globally. The same instructions work for the PATH variable, with one caveat - you should not remove the existing PATH contents, but add to them (using ; to separate paths).



    to which C:\Program Files\Java\jdk1.5.0_06\bin is to be added using ;. Will this allow future software installations (with their own JDKs/JREs) -

    Rob Spoor wrote:By adding the JDK folder to the start you will make sure that no other JDK (or JRE) will be preferred.



    - which I had not thought of (sorry) without interference from jdk1.5.0_06 and vice-versa? It makes most sense to set the path for the duration of the command window session only (as suggested by Brian).


    Brian - thank you for this suggestion.

    Brian Burress wrote:Some options which can help you avoid this include setting up a directory of batch files in your path that you can use to set up the path for the command window you have opened. The batch files will add the jdk to the path for your command window session only - when closed they go away. For example you could have batch files for "JDK6", "JDK7", "IBMJDK", etc depending on with what JDK you want to work.



    1. How do I set up a directory of batch files within the specified path? and
    2. by path are you referring to the path leading to the (.java) source file directory (and not the bin directory).

    Awaiting your response.

    thanks and regards,
    Sudhir
     
    Brian Burress
    Ranch Hand
    Posts: 143
    • Likes 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Sudhir,

    You are welcome. I'll elaborate here, let me know if something remains unclear.

    I tend to use a simple text editor like notepad or wordpad for creating a bat file. There are a lot of sites that can offer assistance. Do a web search using criteria like “creating a windows bat file”. These are a couple of sites that popped up for me which should help you.

    commandwindows.com

    wikihow

    Place the “bat” files in a directory which is on your path meaning the “Windows OS” . From a windows command line the command “echo %PATH%” will show you the directories defined in the path. While you can pick one of them to put the bat files in, I would recommend creating a new directory just for your files and add it to the path. If you happen to choose an existing path entry that is deleted for example by a software un-install then you


    The key is to make sure the directory you add to the path directly has the java executables. Below are just examples, so you’ll need to determine where java is located on your machine.

    Jdk16.bat



    jdk15.bat




    Open the command window, change to the desired directory, execute the desired bat file and from there the java executables are “exposed” so you can just type “java <<qualified classname>>” or java along with whatever command line parms you need.

    As an example, the package for a class named 'SomeClass' is 'test' and you compile to a classes directory.
    Assuming you have changed to the classes directory then you should be able to run using ‘java –cp . test.SomeClass’.


     
    Sudhir Srinivasan
    Ranch Hand
    Posts: 93
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Brian Burress wrote:Sudhir,

    You are welcome. I'll elaborate here, let me know if something remains unclear.



    Hi Brian,

    If I've understood your explanation correctly, do the following steps

    Step 1- Add the directory c:\progra~1\java\jdk1.5.0_22\bin (containing the java executables) as value to the path variable (under Environment variable > System variable).......directory path in my computer is the same

    Step 2 - Open command window and change to the above specified directory

    Step 3 - Execute the batch file

    Step 4 - run the program i.e java <<qualified classname>> along with command line arguments if any

    to run programs from the command line.

    To implement step 3 requires creating the batch file first with its command line instructions to run a java application from within the file. Here, the link commandwindows.com provided by you is detailed and useful (thank you) but would need some time to go through it entirely (before putting it to practice).

    In the interim, I researched on the internet and came up with the following code



    which is to be saved in a notepad as .bat file. However, this requires the source (.java file) to be in the same directory as the .bat file which would be the bin sub-directory. This also requires the batch file to be double-clicked from this directory (instead of executing from command prompt).

    In my case, the source files are in a different directory. My question is

    Can the code,lines 5 to 7 shown above, in the batch file be modified to

    a) reference the source from its directory(i.e list all the source files)
    b) match the class name entered by the user to the source name (perhaps String comparison)
    c) then execute the javac, java commands within the batch

    where a), b) and c) would be sub-steps leading to Step 3 i.e run the batch from the command prompt.

    thanks,
    Sudhir
     
    Brian Burress
    Ranch Hand
    Posts: 143
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Sudhir Srinivasan wrote:Can the code,lines 5 to 7 shown above, in the batch file be modified to



    You should be able to update the bat file to help orchestrate pretty much whatever is done from a command line. I would advise you avoid getting to fancy with the bat file unless you know bat well otherwise you are going to find yourself debugging windows scripting instead of working with the java which I think the latter is your intended focus right now.

    Be careful with line 8 from your copied example as it appends the java bin directory to the PATH variable instead of pre-pending it. If you happen to have another java bin on the path variable already then the one you are adding will not be used.

    I think your summary is accurate. Step three is the key in that it will allow you to execute the bat file to set your java bin from any directory you are in as opposed to needing to locate the bat file in the directory with your source as you note in your "interim" example.

     
    Sudhir Srinivasan
    Ranch Hand
    Posts: 93
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Brian Burress wrote:I would advise you avoid getting to fancy with the bat file unless you know bat well otherwise you are going to find yourself debugging windows scripting instead of working with the java which I think the latter is your intended focus right now.



    I know this seems contrary to your advise - kindly bear with me - but in my opinion, gaining additional knowledge along the way is good without losing sight of the original content (in my case java as you've correctly pointed out). And without trying out, more knowledge cannot be gained. True, this is my first exposure to batch scripting but I would like to complete what I've started.

    Brian Burress wrote:Be careful with line 8 from your copied example as it appends the java bin directory to the PATH variable instead of pre-pending it. If you happen to have another java bin on the path variable already then the one you are adding will not be used.



    I've removed this line as I've already added the java bin directory to the PATH variable (under Environment variables > System variables).

    Brian Burress wrote:You should be able to update the bat file to help orchestrate pretty much whatever is done from a command line.



    Which I've done by modifying the batch script in my earlier post to the current



    one. On running the bat, jdk15, from the command line



    the intended .java file ConvertDecimal2Binary is compiled and executed from within the batch - by the way returns integer and other error codes (lines 14-102)which
    I'm unable to handle.

    Also, when the bat is executed again



    within the same command window session, apart from the previously mentioned windows error codes, the exceptions in lines 98, 99 and 102 (does not read the source and is unable to
    find the corresponding class in the second execution ??) are returned. So, I'm faced with two
    issues that require a workaround:
    1) the integer codes with error messages and
    2) the exceptions raised

    Could any of the forum moderators help in resolving this so that I could close this thread (without getting too involved in debugging windows batch scripting thereby deviating from java, as pointed out by Brian).

    thanks,
    Sudhir
     
    Rob Spoor
    Sheriff
    Posts: 22781
    131
    Eclipse IDE Spring VI Editor Chrome Java Windows
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    "dir" returns a lot of meta information. Try "dir /b" instead, this will print only the file names.
     
    With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
    reply
      Bookmark Topic Watch Topic
    • New Topic