Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8166809
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T20:00:30+00:00 2026-06-06T20:00:30+00:00

This is my first question on stackoverflow so I’ll try to keep it concise

  • 0

This is my first question on stackoverflow so I’ll try to keep it concise and relevant.

I’m currently creating a Java program that is attempting to call an external program located on the system, in order to do this however I am required to call a shell script that sets up links to the relevant libraries to ensure that the system is linked to these before the external program can be executed.

The issue at hand is that I cannot invoke the shell script through Java, I’ve researched high and low and am aware that of alternative ways such as the use of the ProcessBuilder class. Unfortunately I’m quite new to the world of trying to invoke command line statements through Java and so I’m stuck for answers.

An example of the code I am using can be found below:

private void analyse_JButtonActionPerformed(java.awt.event.ActionEvent evt) {                                                

// Get project path for copying of Fortran program to folder and execution

String projectPath =  Newproject_GUI.getProjectPath();


String sourcePath [] = {"/bin/sh ", "-c ","source ~/set_env_WRF_gnu.sh"} ;



Runtime fortranAnalyser = Runtime.getRuntime();

try {
        Process p = fortranAnalyser.exec("cp main.exe " + projectPath);
        Process k = fortranAnalyser.exec(sourcePath);



        BufferedReader is = new BufferedReader(new InputStreamReader(k.getInputStream()));
        String line;
        while ((line = is.readLine()) != null) {

            System.out.println(line); 
        } 


    } catch (IOException ex) {
        Logger.getLogger(Analyser_GUI.class.getName()).log(Level.SEVERE, null, ex);
      }

}

Process p works fine and does indeed copy main.exe to the intended directory when the method is called. Process k however does not and this is where the issue is.

Thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T20:00:33+00:00Added an answer on June 6, 2026 at 8:00 pm

    The issue is “source” is internal command of BASH (you are using “sh” but that is just BASH in the simplified mode). So what you do is:

    • you spawn new process “sh” and source something there (setting some VARIABLES I guess)
    • the process ends and all VARIABLES are lost
    • you spawn another process, but VARIABLES are already gone

    I am not sure if you use those variables later on, but according to the script name it is probably setting some. Don’t do that like this.

    By the way if you only want to execute script in bash, you don’t need to source it. To get it’s side effects, just execute it with:

    String sourcePath [] = {"/bin/sh ", "/home/XYZ/set_env_WRF_gnu.sh"} ;
    

    Please note you cannot use ~ in this case, use Java to get your home dir.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is my first question in StackOverflow, so I would try to explain my
This is my first question on stackoverflow! I'm currently programming a steamcommunity crawler, which
This is my first stackoverflow question, so try to be nice. ;-D My issue
This is my first question here on stackoverflow, so I hope that I am
This is my first question of stackoverflow! I've got a PyQT gui that's displaying
Hi, I'm a Rails newbie and this is my first stackoverflow question. I'm currently
this is my first question on StackOverflow, but I think that we'll both come
This is my first question on stackoverflow. I just wonder why my getJSON code
this is my first time asking a question on stackoverflow. I'm working on a
First of all, sorry if this isn't an appropriate question for StackOverflow. I've tried

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.