search
Homephp教程PHP开发Dynamically set environment variables of the Linux system through java code

What we are talking about today is to dynamically set environment variables of the Linux system through java code. To be honest, I have been searching on Google for a long time in the past two days and have not been able to find out how to do it. Maybe it is not very useful in practice. But after searching for a long time, I will record it now that I have solved it. Come on

Java code

import java.io.BufferedInputStream;  
import java.io.File;  
import java.io.IOException;  
import java.io.InputStream;  
  
public class ExecuteCmd {  
  
    /** 
     * @param args 
     */  
    public static void main(String[] args) {  
        String[] commonds = {"sh","-c","export JAVA_HOME=/usr/java/jdk;echo $JAVA_HOME"};  
        RunSystemCommand(commonds, null);  
    }  
  
    public static void RunSystemCommand(String[] command, File file) {  
        if (command != null && !command.equals("")) {  
            try {  
                Process ps = null;  
                if (file != null)  
                    ps = Runtime.getRuntime().exec(command, null, file);  
                else  
                    ps = Runtime.getRuntime().exec(command);  
                String message = loadStream(ps.getInputStream());  
                String errorMeg = loadStream(ps.getErrorStream());  
                System.out.println(message);  
                System.out.println("-------");  
                System.out.println(errorMeg);  
                try {  
                    ps.waitFor();  
                } catch (InterruptedException e) {  
                    e.printStackTrace();  
                }  
            } catch (IOException e) {  
                e.printStackTrace();  
            }  
        }  
    }  
  
    private static String loadStream(InputStream in) throws IOException {  
        int ptr = 0;  
        in = new BufferedInputStream(in);  
        StringBuffer buffer = new StringBuffer();  
        while ((ptr = in.read()) != -1) {  
            buffer.append((char) ptr);  
        }  
        return new String(buffer.toString().getBytes("ISO-8859-1"), "GBK");  
    }  
}

In fact, the most important sentence is this
String[] commonds = {"sh","-c","export JAVA_HOME=/usr/java/jdk;echo $JAVA_HOME"};
How to write this command. Others can be found on Google. And according to my test, these cannot be written together. For example,
String tmp_run_cmd = "sh -c 'export JAVA_HOME=/usr/java/jdk;echo $JAVA_HOME'" ; //This is not possible..


For more related articles about dynamically setting environment variables of the Linux system through java code, please pay attention to the PHP Chinese website!


Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
详细介绍如何在Windows 11上打开环境变量设置详细介绍如何在Windows 11上打开环境变量设置Dec 30, 2023 pm 06:07 PM

环境变量功能是系统中的配置程序运行必备工具,但是在最新的win11系统中还有许多的用户不知道怎么设置打开,下面就给你们带来了win11环境变量打开位置详细介绍,快来一起学习操作一下吧。win11环境变量在哪:1、首先输入“win+R”,打开运行框。2、然后在里面输入命令:controlsystem。3、在打开的系统信息界面中,选择左侧菜单的“高级系统设置”。4、随后在打开的“系统属性”窗口选择下方的“环境变量”选项。5、最后在打开的环境变量中,即可根据需求进行相关的设置。

设置Linux系统的PATH环境变量步骤设置Linux系统的PATH环境变量步骤Feb 18, 2024 pm 05:40 PM

Linux系统如何设置PATH环境变量在Linux系统中,PATH环境变量用于指定系统在命令行中搜索可执行文件的路径。正确设置PATH环境变量可以方便我们在任何位置执行系统命令和自定义命令。本文将介绍如何在Linux系统中设置PATH环境变量,并提供详细的代码示例。查看当前的PATH环境变量在终端中执行以下命令,可以查看当前的PATH环境变量:echo$P

解决Java环境变量配置未生效的方法解决Java环境变量配置未生效的方法Feb 19, 2024 pm 10:57 PM

如何解决Java环境变量配置后不生效的问题在Java开发过程中,我们经常需要配置Java环境变量来确保程序的正常运行。然而有时候我们会遇到一些奇怪的问题,即使正确配置了Java环境变量,却发现程序并没有按照配置的方式进行运行。这其实是一个常见的问题,本文将介绍一些解决方法,并提供具体的代码示例。问题的根源在于Java环境变量的配置没有正确生效。下面是一些常见

windows10环境变量设置教程windows10环境变量设置教程Jul 15, 2023 pm 06:09 PM

环境变量是操作体系中的一个重要设置,可是有的windows10用户还不清楚环境变量应该怎样设置。其实办法很简单,翻开运转窗口,经过sysdm.cpl指令调出体系特点窗口,之后切换到高档选项卡,就能看到有个环境变量的按钮了,点击这个按钮就能进入环境变量的设置界面了,之后根据实际需要去设置就行了。windows10环境变量怎样设置:1、首先在电脑上翻开运转窗口,接着在窗口内输入sysdm.cpl然后回车。2、在体系特点界面内挑选高档,然后点击环境变量。3、在这里咱们可以看到所显现的变量,单机新建就能

如何配置Tomcat环境变量如何配置Tomcat环境变量Oct 26, 2023 am 10:41 AM

配置Tomcat环境变量需要在系统中添加CATALINA_HOME变量,并将Tomcat的安装路径添加到PATH变量中。 在windows中的步骤是先下载和安装Tomcat、打开系统属性窗口、打开环境变量设置、添加Tomcat环境变量、修改Path变量和验证配置。在linux中的步骤是先下载和安装Tomcat、打开终端窗口、编辑bashrc文件、添加Tomcat环境变量等等。

简单易懂的PyCharm环境变量配置指南简单易懂的PyCharm环境变量配置指南Feb 21, 2024 pm 03:03 PM

PyCharm是一款功能强大的Python集成开发环境(IDE),它提供了丰富的功能和工具,可以帮助开发者提高工作效率。在PyCharm中,可以通过配置环境变量来方便地管理项目中的配置信息,本文将为大家介绍如何在PyCharm中进行环境变量的配置,希望对初学者和开发者有所帮助。一、在PyCharm中配置环境变量的步骤如下:打开你的PyCharm项目,在项目面

配置python环境变量的方法是什么配置python环境变量的方法是什么Aug 21, 2023 pm 02:10 PM

通过下载Python安装包、安装Python、配置环境变量和验证配置的方法配置python环境变量。详细介绍:1、打开控制面板;2、点击"系统和安全";3、点击"系统";4、点击"高级系统设置";5、点击"环境变量";6、配置用户环境变量;7、添加Python路径;8、配置系统环境变量;9、添加Python路径到系统变量;10、确认配置即可。

内部错误:无法创建临时目录 [已解决]内部错误:无法创建临时目录 [已解决]Apr 17, 2023 pm 03:04 PM

Windows系统允许用户使用可执行/设置文件在您的系统上安装各种类型的应用程序。最近,许多Windows用户开始抱怨他们收到一个名为INTERNALERROR:cannotcreatetemporarydirectory在他们的系统上尝试使用可执行文件安装任何应用程序的错误。问题不仅限于此,而且还阻止用户启动任何现有的应用程序,这些应用程序也安装在Windows系统上。下面列出了一些可能的原因。运行可执行文件进行安装时不授予管理员权限。为TMP变量提供了无效或不同的路径。损坏的系

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools