This week's book giveaways are in the Angular and TypeScript and Web Services forums.
We're giving away four copies each of Programming with Types and The Design of Web APIs and have the authors on-line!
See this thread and this one for details.
Win a copy of Programming with Types this week in the Angular and TypeScript forum
or The Design of Web APIs in the Web Services forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Knute Snortum
  • Henry Wong
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Frits Walraven
  • Joe Ess
  • salvin francis

How I do get system path enviroment variable?

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

I have to get the value of the system path variable in Windows 10
I've tried  with the code provided below :




but the output is very different from what I can see in the windows "enviroment variables", please see attachment image

This is the output from Java !


C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Windows\System32\OpenSSH\;C:\Program Files\java\jdk-13.0.1\bin;C:/Program Files/Java/jdk-13.0.1/bin/server;C:/Program Files/Java/jdk-13.0.1/bin;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Windows\System32\OpenSSH\;C:\Program Files\java\jdk-13.0.1\bin;C:\Users\Arthur\AppData\Local\Microsoft\WindowsApps;;C:\eclipse2019-12;;C:\eclipse2019-12\Workspace\jacis-maven\apache-ant-1.10.7in



My pourpose is add a new path to PATH variable

Can you explain me please?
Thanks
path.png
[Thumbnail for path.png]
 
Sheriff
Posts: 6551
176
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Path that you get from java getenv() (and also from getting to a command line and typing PATH) are a combination of the system environment PATH and the user PATH.  If you're the only one on your PC then it doesn't matter which PATH variable you put your path to Java into, but put it into the user one just to be safe.

I can see you've made several attempts to put Java into your PATH.  You only need one, so clean up the others.
 
Melk Lo
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
oh ! yes there are two different account. There is an Admin account and a normal user (me) .Now I see. Thanks

I want to add ANT_HOME to enviroment variables, but I found several post on coderanch about this topic and I understood it's very difficulty especially
for a beginner. for example, If I write this in cmd :

setx ANT_HOME c:\....  It works and I can see the new variable in "User variable"

but If I do the same from Java , it doesn't work


It returns no error but it doesn't affect user variables.
I only understood that It doesn't work becouse it's not possible do it from the same process..Is It true ?


I'm going to create a batch file from Java. I know that is not a elegant solution..But It's going to work only for a single account

 
Marshal
Posts: 66980
255
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
. . . and welcome to the Ranch
 
Saloon Keeper
Posts: 21437
143
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you're trying to update PATH in Java code, I think you may have a basic problem.

As others have noted, there are two environments in Microsoft Windows™. One is for the entire OS, the other is user-specific and the two basically merge to obtain the effective environment.

What may be a bigger issue is that changes made in a process to the environment may not propagate up and out of that process.

In the Unix-like OS's (such as Linux), a completely separate copy of the environments is generally created (and destroyed) for each process. So if a shell script runs another shell script, the second script's changes to its environment will not be passed back to the calling script unless the called script does some special magic (the "export" statement).

I'm not positive, but I think that Windows acts somewhat the same.

In any event, if you want a Java application to set ANT_HOME so that it can then call Ant, that's not the best way to do it. Ant, like many Java utilities, is implemented around a core JavaBean class. If you instantiate that, set its properties as desired, and then invokes its main method, you can run Ant without messing with the environment at all and without the overhead that Runtime.exec() and its relatives requires. Or, if you must use Runtime.exec(), make a .BAT file with the proper settings and invoke that.

If all you are trying to do is permanently set/alter the user or system environment location for ANT_HOME, Windows installer programs generally do that sort of stuff, and for general OS manipulation, I recommend the formal package management channels over custom applications (I even deploy my WARs as OS-managed packages, though!) Or, if you must, I'll just have to refer you to the Windows API, since unlike Unix/Linux, I don't think there's a profile text file for the environment that you can manipulate by brute force.
 
Melk Lo
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:. . . and welcome to the Ranch



Thanks Ritchie . I'am Giorgio from Italy and I started studying some weeks ago , nice to meet you !


Tim Holloway wrote:If you're trying to update PATH in Java code, I think you may have a basic problem.....



Hi Tim , I read you long answer and I want thank you for that. Last night I managed to write enviroment variable by another way.
I've had two problems :
1) update the path variable and create one ( ANT_HOME)
2) call ant

I found JNI. to solve the first one. It works as you can see the image below.
the code is :

I don't think to solve both problems by JNI because I feel there is another problem here as you told me
I noticed something :
It works only with Admin account, may be that's what you're talling to me. see the attachment please :

User.png -> updated enviroment variables but I doesnt'work
Admin.png -> updated enviroment variables ( by hand) It Works.
Now I can update the enviroment variables but It works only with Admin account
User.png
[Thumbnail for User.png]
Admin.png
[Thumbnail for Admin.png]
 
Melk Lo
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm sorry I forgot to add JAVA_HOME in normal user account variables

Now ,it recognises Ant but returns another error .. "Home is set incorrectly or ant could not be located. Please set ANT_HOME

 
Knute Snortum
Sheriff
Posts: 6551
176
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This isn't going to work well if you call it repeatedly.  You will keep adding the Ant path to your PATH variable.  You should check to see if the Ant path is already in the userPath.

I think this will work, as the entire ANT_HOME variable will be overwritten (if that's what you're trying to do).
 
Tim Holloway
Saloon Keeper
Posts: 21437
143
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since you used JNI, that pretty much implies that yes you ended up using a Windows API function.

However, since you are calling Ant, I really do recommend using the Ant JavaBean instead of all that OS-specific rigamarole.

I think this might help:

http://www.srccodes.com/p/article/9/Invoke-and-Execute-Hello-World-Ant-Script-Programmatically-using-Java-Code

The advantages of running an embedded copy of Ant are less overhead, no need to fiddle around with environment variables, and portability. The example given should run unmodified on Windows, MacOS and Linux. Also, you don't a need foreign-language development environment - if I'm not mistaken, you had to write your JNI code in C or something like that.
 
Knute Snortum
Sheriff
Posts: 6551
176
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Melk Lo: Please don't post screenshots.  Copy and paste the commands into the post.  You can copy text in a command prompt by highlighting it and pressing <Enter>.
 
Tim Holloway
Saloon Keeper
Posts: 21437
143
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Plus, as Knute said, you have to be more careful when meddling with the environment, which isn't an issue for embedded Ant, since it doesn't use the environment at all.

Although I almost forgot - of course you do need the ANT_HOME directory on your classpath so that Ant can find its task libraries. An application can set its classpath dynamically in Java code, however, so still no need to meddle with the environment even if you didn't have Ant's path in your controlling app's classpath originally.
 
Melk Lo
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ,

I solved my problem and I came to "conclusion" it's preferabbly write to register without enviroment panel.
Windows 10 doesnt need to restart OS but in my humble opinion , the enviroment panel is confusing.
for example : I only opened the panel and close it last time and then all of a sudden it worked!Just before I had tried to open close cmd and restart windows explorer but with no success.


Tim Holloway wrote:
I think this might help:

http://www.srccodes.com/p/article/9/Invoke-and-Execute-Hello-World-Ant-Script-Programmatically-using-Java-Code

The advantages of running an embedded copy of Ant are less overhead, no need to fiddle around with environment variables, and portability. The example given should run unmodified on Windows, MacOS and Linux. Also, you don't a need foreign-language development environment - if I'm not mistaken, you had to write your JNI code in C or something like that.



I have to compile the code cloned from repository (GIT) , Should I edit it ?

Thank to all
 
Tim Holloway
Saloon Keeper
Posts: 21437
143
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Melk Lo wrote:
I have to compile the code cloned from repository (GIT) , Should I edit it ?



Well, you'll want to use your own project, and probably some of the paths should be changed to match your filesystem. But it looked to me like you should be able to take it and use it mostly unchanged.
 
crispy bacon. crispy tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!