Hi Nuwan,

System.getenv() is working fine with windows environment but not so well in
linux environments.
Please utilize System.getProperty() and check again.

If the above change didn't work try getting all the available properties
using the code given below.

public static void main(String[] args) {
>         RuntimeMXBean runtimeBean = ManagementFactory.getRuntimeMXBean();
>
>         Map<String, String> systemProperties =
> runtimeBean.getSystemProperties();
>         Set<String> keys = systemProperties.keySet();
>         for (String key : keys) {
>             String value = systemProperties.get(key);
>             System.out.printf("[%s] = %s.\n", key, value);
>         }
>     }

If you can't not see the $M2_HOME in the property list that means the
system property is not set in the scope where java searches for the system
variables.

Hope this wil solve the issue.

Note: Its better if you can set the $M2_HOME home in /etc/environments
rather then ~/.bashrc. Since /etc/environment does not depend on the logged
user.

Thanks and Regards.

Kamidu Sachith Punchihewa
*Software Engineer*
WSO2, Inc.
lean . enterprise . middleware
Mobile : +94 (0) 770566749 <%2B94%20%280%29%20773%20451194>


Disclaimer: This communication may contain privileged or other confidential
information and is intended exclusively for the addressee/s. If you are not
the intended recipient/s, or believe that you may have received this
communication in error, please reply to the sender indicating that fact and
delete the copy you received and in addition, you should not print, copy,
retransmit, disseminate, or otherwise use the information contained in this
communication. Internet communications cannot be guaranteed to be timely,
secure, error or virus-free. The sender does not accept liability for any
errors or omissions.

On Mon, Dec 14, 2015 at 12:00 PM, Nuwan Pallewela <nuw...@wso2.com> wrote:

> Hi Devs,
>
> I need to get the *Maven Home* value to execute maven goal
> eclipse:eclipse for a created project. I tried to use *java.lang.System*
> class to retrieve it by executing following code. But Maven Home in not
> listed in there either properties or process environment.
>
> if (System.getenv("M2_HOME") != null) {
>             return System.getenv("M2_HOME");
>         } else if (System.getenv("MAVEN_HOME") != null) {
>             return System.getenv("MAVEN_HOME");
>         } else if (System.getenv("M3_HOME") != null) {
>             return System.getenv("M3_HOME");
>         } else if (System.getProperty("maven.home") != null) {
>             return System.getProperty("maven.home");
>         } else {
>             log.error("Maven Home variable value is not found in system
> properties");
>             throw new IllegalStateException("Maven Home variable is not
> set ");
>         }
>
> Maven is correctly installed in the system and following is the result
> when I run "mvn --version"
>
> nuwan@nuwan-ThinkPad-T540p:~$ mvn --version
> Apache Maven 3.2.5 (12a6b3acb947671f09b81f49094c53f426d8cea1;
> 2014-12-14T22:59:23+05:30)
> Maven home: /usr/local/apache-maven-3.2.5
> Java version: 1.8.0_66, vendor: Oracle Corporation
> Java home: /usr/lib/jvm/java-8-oracle/jre
> Default locale: en_US, platform encoding: UTF-8
> OS name: "linux", version: "3.13.0-68-generic", arch: "amd64", family:
> "unix"
>
> I have also had edit the .bashrc file by adding following lines.
>
> export M2_HOME=/usr/local/apache-maven-3.2.5
> export M2=$M2_HOME/bin
> export PATH=$M2/bin:$PATH
>
> But still I can't read M2_HOME from java.lang.System class.
>
> Any help would be hugely appreciated.
>
> Thanks,
> Nuwan
>
> --
> ----------------------------------------------------------
>
> *Nuwan Chamara Pallewela*
>
>
> *Software Engineer*
>
> *WSO2, Inc. *http://wso2.com
> *lean . enterprise . middleware*
>
> Email   *nuw...@wso2.com <nuw...@wso2.com>*
> Mobile  *+94719079739 <%2B94719079739>@*
>
>
>
> _______________________________________________
> Dev mailing list
> Dev@wso2.org
> http://wso2.org/cgi-bin/mailman/listinfo/dev
>
>
_______________________________________________
Dev mailing list
Dev@wso2.org
http://wso2.org/cgi-bin/mailman/listinfo/dev

Reply via email to