CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 4 1234 LastLast
Results 1 to 15 of 58
  1. #1
    Join Date
    May 2009
    Posts
    115

    Setting Environment Variables

    Hi,

    This isn't exactly a JAVA related question. But, I was wondering if I wanted to create an Environment variable in windows for my .jar file. How would I go about doing it?

    I have a .bat file that contains just the following text:

    set projectEnv = .\

    ... my intention is that I want it to set the ProjectEnv variable to the location from where the .bat while was executed.

    I'm trying to get the location of the variable in JAVA by doing this :

    System.getenv ("projectEnv"); , I do a System.out for the value but I keep getting a null string. I'm guessing this is because I am not currently setting the projectEnv . Any pointers to what is the right syntax for the batch file?

  2. #2
    Join Date
    Jul 2005
    Location
    Currently in Mexico City
    Posts
    568

    Re: Setting Environment Variables

    Wanna install linux on a vacuum cleaner. Could anyone tell me which distro sucks better?

    I had a nightmare last night. I was dreaming that I’m 64-bit and my blanket is 32-bit and I couldn’t cover myself with it, so I’ve spent the whole night freezing. And in the morning I find that my blanket just had fallen off the bed. =S (from: bash.org.ru)

    //always looking for job opportunities in AU/NZ/US/CA/Europe :P
    willCodeForFood(Arrays.asList("Java","PHP","C++","bash","Assembler","XML","XHTML","CSS","JS","PL/SQL"));

    USE [code] TAGS! Read this FAQ if you are new here. If this post was helpful, please rate it!

  3. #3
    Join Date
    May 2009
    Posts
    115

    Re: Setting Environment Variables

    Hey Xeel - Umm ... maybe I'm missing the point of the link but I am doing what was specified
    With a String argument, getEnv returns the value of the specified variable. If the variable is not defined, getEnv returns null. The Env example uses getEnv this way to query specific environment variables, specified on the command line:
    Thus ... System.getenv ("projectEnv"); is fine. However, I'm having problems with setting the environment variable automatically from the location from where the .bat file is run.

    I know how to set an environment variable via My Computer -> Properties -> Environment Variables and then enter the physical location like C:\Windows\projectEnv. But what if I just want to double click the .bat file and make it automatically pass the address depending on where the .bat file was run from?

  4. #4
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: Setting Environment Variables

    Quote Originally Posted by worldChanger View Post
    I have a .bat file that contains just the following text:

    set projectEnv = .\

    ... my intention is that I want it to set the ProjectEnv variable to the location from where the .bat while was executed.
    I think the problem there is that environment variables set in the batch file are local to that batch file. When it finishes, they go away. You need to set the variable in the same batch file that runs the Java app. You can do this by passing the variable as a parameter to the batch that runs the Java app, or by passing the command line that runs the Java app to the batch that sets the environment variable.

    Experience is a poor teacher: it gives its tests before it teaches its lessons...
    Anon.
    Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

  5. #5
    Join Date
    May 2009
    Posts
    115

    Re: Setting Environment Variables

    Hey dlorde - Nopes, still doesn't work. I get a return value of null still. Is SET projectEnv = .\ the right syntax to do what I am aiming for? (ie. Setting projectEnv path to the same location from where it is executed?) ...

  6. #6
    Join Date
    May 2009
    Location
    Lincs, UK
    Posts
    298

    Re: Setting Environment Variables

    Is there a space between projectEnv and the = sign? If so, that is your problem. Windows treats "projectEnv" and "projectEnv " as two different environment variables. You can either remove all spaces both sides of the = sign or use System.getenv("projectEnv ").

  7. #7
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: Setting Environment Variables

    Quote Originally Posted by worldChanger View Post
    Hey dlorde - Nopes, still doesn't work. I get a return value of null still.
    OK - I was somewhat inaccurate in my previous post - the variables set in a batch file are actually local to the command processor the batch is running in.

    Anyhow, if jcaccia's idea doesn't help, perhaps you could post up what you've got so we can see what's going on.

    Don't worry about people stealing your ideas. If your ideas are any good, you'll have to ram them down people's throats...
    H. Aiken
    Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

  8. #8
    Join Date
    May 2009
    Location
    Lincs, UK
    Posts
    298

    Re: Setting Environment Variables

    Do you need that environment variable somewhere else (for something else in your batch file, for instance)? If you plan to use it only to get the directory where the .jar is executed from, you could use System.getProperty("user.dir") instead. Just to clarify, that will give you the current directory when you invoke the .jar, not the directory where the .jar lives.

  9. #9
    Join Date
    May 2009
    Posts
    115

    Re: Setting Environment Variables

    Quote Originally Posted by jcaccia View Post
    Is there a space between projectEnv and the = sign? If so, that is your problem. Windows treats "projectEnv" and "projectEnv " as two different environment variables. You can either remove all spaces both sides of the = sign or use System.getenv("projectEnv ").
    Wow, that was amazing! Just a space! ... that did do the trick !

    ----
    Another question I have:

    I have a Jtable that contains several coloumns. These coloumns contain numeric data, but they are stored as type String unfortunately.

    What I want to do is to search for number falling in a certain range, say between 5 to 10 (so, 5.55, or 8.88) and output them on the screen. What would be the best way of doing it? The way, I see it ... I would need to create a loop that goes through the entire coloumn and adds that data to an array. While adding the data, I could parse it to type double?

    Is there a better solution to do this? Or is that the only way?

  10. #10
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: Setting Environment Variables

    The way you suggest seems reasonable - although you don't necessarily need the intermediate array unless you want to keep the numbers for use elsewhere - you could convert, check the range and output each as a string, skipping the ones that don't match the range criteria - alternatively you could store the strings that match the range criteria. I'd recommend using an ArrayList rather than an array. You can use the Double class constructor to get a Double for the string, or its parseDouble method to get a primitive double. After range checking, you can then either output the original string or reformat the double for output in a different format using DecimalFormat or the PrintStream.printf formatting method.

    It is better to have an approximate answer to the right question than an exact answer to the wrong one...
    J. Tukey
    Last edited by dlorde; July 29th, 2009 at 04:01 AM.
    Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

  11. #11
    Join Date
    May 2009
    Posts
    115

    Re: Setting Environment Variables

    Hi dlorde - Thansk for your reply. I'm going to try this out soon.

    I have an elementary problem though. Say, that I have an array of doubles. (double[] numbers).
    I want to insert this array into the Jtable. However, I want to have 1 cell to the extreme left and 1 cell to the extreme right of the number data.

    Thus, I tried to create an object, Object[] ObjectToAdd = { leftText , numbers , rightText };
    Unfortunately ... this doesn't work. As in, it doesn't display the array contents of v. It only displays a hashcode. Is this because, I am incorrectly passing it into ObjectToAdd ?
    Last edited by worldChanger; July 29th, 2009 at 01:26 PM.

  12. #12
    Join Date
    May 2009
    Posts
    115

    Re: Setting Environment Variables

    Alternatively, I tried adding it as a vector.

    Code:
                        Vector<Object> ObjectToAdd = new Vector<Object>();
                        ObjectToAdd.add(leftText );
                        ObjectToAdd.add(numbers);
                        ObjectToAdd.add(rightText );
                        TableModel.addRow(ObjectToAdd);
    However, that doesn't do it as well.

    Code:
                              Vector<String[]> ObjectToAdd = new Vector<String[]>();
                            ObjectToAdd.add(leftText );
                            ObjectToAdd.add(tableData);
                            ObjectToAdd.add(rightText);
                        TableModel.addRow(ObjectToAdd);
    For the 2nd pieve of code, I get an errror as Left and Right text are merely Strings and not an array of strings.
    Last edited by worldChanger; July 29th, 2009 at 02:00 PM.

  13. #13
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: Setting Environment Variables

    Quote Originally Posted by worldChanger View Post
    Thus, I tried to create an object, Object[] ObjectToAdd = { leftText , numbers , rightText };
    It is not a good idea to mix types in an array.

    Unfortunately ... this doesn't work. As in, it doesn't display the array contents of v. It only displays a hashcode. Is this because, I am incorrectly passing it into ObjectToAdd ?
    No comment - I have no idea what you're doing with it - I'm not a mind reader, but if you want table data as arrays, it must be a 2 dimensional array, e.g. Object[][]. As you have it, it is just an array of three anonymous Objects.

    Programming is an explanatory activity...
    R. Harper
    Please use &#91;CODE]...your code here...&#91;/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

  14. #14
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: Setting Environment Variables

    Quote Originally Posted by worldChanger View Post
    For the 2nd pieve of code, I get an errror as Left and Right text are merely Strings and not an array of strings.
    The addRow method takes an array of cell values (i.e. column values for a single row) - you appear to be trying to add 'numbers' as a single cell (column value).

    I suggest you forget that and supply your own table model by subclassing AbstractTableModel. You can store your data in it however you like, as long as you implement the three methods:
    Code:
    public int getRowCount();
    public int getColumnCount();
    public Object getValueAt(int row, int column);
    The purpose of computing is insight, not numbers...
    R. Hamming
    Please use &#91;CODE]...your code here...&#91;/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

  15. #15
    Join Date
    May 2009
    Posts
    115

    Re: Setting Environment Variables

    Quote Originally Posted by dlorde View Post
    The way you suggest seems reasonable - although you don't necessarily need the intermediate array unless you want to keep the numbers for use elsewhere - you could convert, check the range and output each as a string, skipping the ones that don't match the range criteria - alternatively you could store the strings that match the range criteria. I'd recommend using an ArrayList rather than an array. You can use the Double class constructor to get a Double for the string, or its parseDouble method to get a primitive double. After range checking, you can then either output the original string or reformat the double for output in a different format using DecimalFormat or the PrintStream.printf formatting method.

    It is better to have an approximate answer to the right question than an exact answer to the wrong one...
    J. Tukey
    Hey dlorde - I was trying to figure this out. But I'm getting a few problems. So , basically I have a couple of coloums in my table. But, I only want to get the data from coloumns called "Grades" ... now there might be just 1 coloumn of grades or there might be 2 or more depending on the amount of data loaded.

    What, I want to be able to do is search for the coloumn titled "Grades" ... and change the colours of the marks contained within it. So for example if the student recevied between 0 - 49 ... it should be red ... between 90 - 100 it should be green etc.

    Unfortunately, I'm not sure how to collect the data in the coloumn as an array. Also, its easier if I just have 1 coloumn of grades, I can manually specify the col ... but how do I make the code generic to find even the 2nd or more cols if they do exist?

Page 1 of 4 1234 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured