LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-12-2011, 01:04 AM   #1
tanveer
Member
 
Registered: Feb 2004
Location: e@rth
Distribution: RHEL-3/4/5,Gloria,opensolaris
Posts: 525

Rep: Reputation: 39
change directory in linux with java


Hi all,
I am trying to run the configure command to install a package in linux with Java. Now my workspace location of Eclipse project is /home/user/Workspace/projectx. The source package say zebra is located in /project/test/zebra. Now there is a configure script which I want to run using java and all the outout files will also be created in /project/test/zebra.
Now in Java the code I am using runs the configure command but all the files it creates in current workspace of project which is in /home/user/Workspace/projectx directory.
Now as I tried to do 'cd' command but as it's internal command of linux so it doesn't work and I don't know how to make it work.
Thanks.

Below is the part of the code:
PHP Code:
    private static void createMakefile(String confDir) {
        
        
System.out.println ("Path " confDir );
        
        
// Changing to package directory
        
String changeDirectory "/bin/bash cd " confDir;
        
        
// setting up command to configure
        
String commandFile confDir "/configure" ;

        try {
            
int ch;
           
            
Runtime.getRuntime().exec(changeDirectory);
            
            
Process shellProcess Runtime.getRuntime().exec(commandFile);
            
            
shellProcess.waitFor();
        
InputStreamReader myIStreamReader = new
             
InputStreamReader(shellProcess.getInputStream());
             while ((
ch myIStreamReader.read()) != -1) {
             
System.out.print((char)ch);
             }
            

        } catch (
IOException anIOException) {
            
System.out.println(anIOException);
        }
        catch (
InterruptedException e) {
            
System.out.println(e);
        }
        catch(
Exception e)
        {
            
e.printStackTrace();
        
        }

    } 
 
Old 03-12-2011, 01:13 AM   #2
corp769
LQ Guru
 
Registered: Apr 2005
Location: /dev/null
Posts: 5,818

Rep: Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007
I'm sure you can use chdir instead.
 
Old 03-12-2011, 01:23 AM   #3
RASCRanasingha
LQ Newbie
 
Registered: Mar 2011
Location: Talpittiya, Vaadduva, Shri Langka
Distribution: Kubuntu
Posts: 8

Rep: Reputation: 1
Hi Tanveer...

Try Using "/bin/bash -e /bin/cd " instead...
 
0 members found this post helpful.
Old 03-12-2011, 01:23 AM   #4
tanveer
Member
 
Registered: Feb 2004
Location: e@rth
Distribution: RHEL-3/4/5,Gloria,opensolaris
Posts: 525

Original Poster
Rep: Reputation: 39
hey thanks I totally forgot this command.
But the problem is it's not recognised in Ubuntu which I am currently working on.
Any other way ?
 
Old 03-12-2011, 08:06 AM   #5
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,780

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
The current directory is different for every process, so you can't change directory inside one process and expect it to change directory for another. You can use java's ProcessBuilder to set the working directory of an executed command:

Code:
ProcessBuilder pb = new ProcessBuilder(confDir + "/configure");
pb.directory(new File(confDir));
p1 = pb.start()
 
Old 03-13-2011, 05:19 AM   #6
tanveer
Member
 
Registered: Feb 2004
Location: e@rth
Distribution: RHEL-3/4/5,Gloria,opensolaris
Posts: 525

Original Poster
Rep: Reputation: 39
thanks to all and big thanks to ntubski.
It worked like charm.
I changed the code as follows:
PHP Code:
private static void createMakefile(String confDir) {
        
        
System.out.println ("Path " confDir );
        
        
String commandFile =  confDir "/a.sh" ;
        
        try {
            
int ch;
            
            
ProcessBuilder pb = new ProcessBuilder(confDir+"/a.sh");
            
pb.directory(new File(confDir));
        
                
Process shellProcess pb.start(); 
            
            
shellProcess.waitFor();
            
            
InputStreamReader myIStreamReader = new
             
InputStreamReader(shellProcess.getInputStream());
           
             while ((
ch myIStreamReader.read()) != -1) {
             
System.out.print((char)ch);
             }
        } catch (
IOException anIOException) {
            
System.out.println(anIOException);
        } catch(
Exception e) {
            
e.printStackTrace();
        
        }

    } 
 
Old 03-15-2011, 06:34 AM   #7
tanveer
Member
 
Registered: Feb 2004
Location: e@rth
Distribution: RHEL-3/4/5,Gloria,opensolaris
Posts: 525

Original Poster
Rep: Reputation: 39
hello,

Got stuck in one more part and need help.
I am trying to add CFLAGS environment value but its not compiling.

In the below code I just changed this line
PHP Code:
ProcessBuilder pb = new ProcessBuilder(confDir+"/configure"); 
to this
PHP Code:
ProcessBuilder pb = new ProcessBuilder("CFLAGS=-xyz" " " confDir "/configure"); 
It's giving me this error:
Code:
Cannot run program "/test/package/configure CFLAGS=-xyz" (in directory "/test/package"): java.io.IOException: error=2, No such file or directory
But it runs from command prompt. I also tried putting the CFLAGS value at then end of configure which also gives same error but that also runs from command prompt.
 
Old 03-15-2011, 07:13 AM   #8
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,780

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
configure can take environment variable as arguments so you could do:

Code:
ProcessBuilder pb = new ProcessBuilder(confDir + "/configure", "CFLAGS=-xyz");
Or, the direct equivalent of the shell's setting the environment:

Code:
ProcessBuilder pb = new ProcessBuilder(confDir + "/configure");
pb.environment().put("CFLAGS", "-xyz");
 
1 members found this post helpful.
Old 03-21-2011, 04:13 PM   #9
tanveer
Member
 
Registered: Feb 2004
Location: e@rth
Distribution: RHEL-3/4/5,Gloria,opensolaris
Posts: 525

Original Poster
Rep: Reputation: 39
Thanks it worked. I should spend some more time on studying documentation of these classes.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
how to change keyboard locale using java in linux linus_trivaldo Programming 1 06-02-2009 07:45 AM
how to change keyboard locale using java in linux linus_trivaldo Linux - General 0 06-02-2009 07:34 AM
change working directory in java mcshen Programming 1 11-30-2004 02:09 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:26 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration