Results 1 to 5 of 5
Like Tree1Likes
  • 1 Post By noobplus

Thread: Running executable through Java on linux

  1. #1
    otacon's Avatar
    otacon is offline Member
    Join Date
    Dec 2010
    Posts
    24
    Rep Power
    0

    Default Running executable through Java on linux

    Hey guys,

    I'm running into a likely silly issue.

    I need to execture a complied c file, in my java code, and need to do it in linux (newest ubuntu distro)

    Narrowing it down to an example:

    C code:
    Java Code:
    #include<stdio.h>
    
    main()
    {
        printf("Hello World");
        FILE *exeFile;
        exeFile = fopen("myFile","w+");
        fclose(exeFile);
    }
    and my java code running it:
    Java Code:
    ...
    Runtime r=Runtime.getRuntime();
    Process p = null;
    String s ="/home/otacon/Desktop/exeTest";
    p=r.exec(s);
    ...
    Where exeTest is the compiled c file.

    Executing the file directly generates the print, and the empty file 'myFile', which is what I want. However, running it through Java (Eclipse) it runs but does absolutely nothing. At first I thought I might just not be getting the output, which is why I added the file creation, but that is not created either.

    Thanks in advance for the help!

  2. #2
    otacon's Avatar
    otacon is offline Member
    Join Date
    Dec 2010
    Posts
    24
    Rep Power
    0

    Default Re: Running executable through Java on linux

    Well, just an update, I managed to fix it for now, but only in windows,

    Compiling the C code in Windows, and running
    Java Code:
    Runtime.getRuntime().exec("C:\\exeFile");"
    does the trick for now. But I still need to figure out how to accomplish this under linux.

    Thanks!
    --Otacon
    Somebody set up us the bomb.

  3. #3
    otacon's Avatar
    otacon is offline Member
    Join Date
    Dec 2010
    Posts
    24
    Rep Power
    0

    Default Re: Running executable through Java on linux

    OH.MY.GOD. *facepalm*

    So yea, the code in my original post works just fine (linux and win). Of course the text output doesn't show up in Eclipse console, which i anticipated, but I brainfarted and didn't think that the file will be created in the Eclipse workspace, not where the compiled c file was located -_- (unlike it is when running it directly)

    Way to loose 4 hours.

    Sorry for wasting your time!
    --Otacon
    Somebody set up us the bomb.

  4. #4
    noobplus's Avatar
    noobplus is offline Senior Member
    Join Date
    Feb 2012
    Location
    chennai,south of India(south east of asia)
    Posts
    232
    Rep Power
    3

    Default Re: Running executable through Java on linux

    Quote Originally Posted by otacon View Post
    Of course the text output doesn't show up in Eclipse console,
    the text output can be brought into the eclipse console by taking the process into a buffered reader.

    example,
    Java Code:
    Process p = Runtime.getRuntime().exec("echo Hi it's me dhilip");
    BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
    String s = null;
     while ((s = stdInput.readLine()) != null) {
                        System.out.println(s);
                    }
    Edited to add: output will be
    Java Code:
    Hi it's me dhilip
    regards
    dhilip
    Last edited by noobplus; 03-27-2012 at 07:15 PM.
    otacon likes this.

  5. #5
    otacon's Avatar
    otacon is offline Member
    Join Date
    Dec 2010
    Posts
    24
    Rep Power
    0

    Default Re: Running executable through Java on linux

    Quote Originally Posted by noobplus View Post
    the text output can be brought into the eclipse console by taking the process into a buffered reader.

    example,
    Java Code:
    Process p = Runtime.getRuntime().exec("echo Hi it's me dhilip");
    BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
    String s = null;
     while ((s = stdInput.readLine()) != null) {
                        System.out.println(s);
                    }
    Edited to add: output will be
    Java Code:
    Hi it's me dhilip
    regards
    dhilip
    Oh, that's quite handy and very useful for me, actually. Thank you!
    --Otacon
    Somebody set up us the bomb.

Similar Threads

  1. Running Executable jar
    By yogi1410 in forum Advanced Java
    Replies: 12
    Last Post: 04-21-2010, 05:46 PM
  2. Errors running app in linux but not in Windows
    By JohnST in forum New To Java
    Replies: 3
    Last Post: 03-12-2010, 02:58 AM
  3. Replies: 3
    Last Post: 01-21-2010, 02:51 AM
  4. Running Linux
    By selva.bics in forum AWT / Swing
    Replies: 1
    Last Post: 08-31-2009, 08:01 PM
  5. Running Tomcat in Linux from Eclipse.
    By Felissa in forum Eclipse
    Replies: 1
    Last Post: 06-05-2007, 06:47 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •