Results 1 to 11 of 11
  1. #1
    manou2324 is offline Member
    Join Date
    Mar 2013
    Posts
    56
    Rep Power
    0

    Default Run a c program through java

    Hi,

    I really like this forum because i always find the solution here.
    I hope i will get a solution to run my "C" program through my "Java" program.
    how can i do that?
    (my "C" code contains many many fontions so that i can't write it here for you)

    I use devC++ for the c program.
    And eclipse for my java program.

    thank you

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,299
    Blog Entries
    7
    Rep Power
    24

    Default Re: Run a c program through java

    Have a look at one of the Runtime.exec( ... ) methods.

    kind regards,

    Jos
    The only person who got everything done by Friday was Robinson Crusoe.

  3. #3
    manou2324 is offline Member
    Join Date
    Mar 2013
    Posts
    56
    Rep Power
    0

    Default Re: Run a c program through java

    i didn't find

  4. #4
    monkeyjr97 is offline Senior Member
    Join Date
    Nov 2012
    Posts
    257
    Rep Power
    4

    Default Re: Run a c program through java

    quick google of the words

    "Java Runtime.exec" would solve your issue

    Runtime (Java 2 Platform SE 5.0) here is the Runtime Class API.

  5. #5
    manou2324 is offline Member
    Join Date
    Mar 2013
    Posts
    56
    Rep Power
    0

    Default Re: Run a c program through java

    My C program is named pfe and the executable is pfe.exe
    I wrote this code in eclipse and run it but it doesn't anything and any exception.



    Java Code:
    public class HelloWorld {
    	public static void main(String []args) 
    	{
    
    	Runtime runtime = Runtime.getRuntime();
    	try {
    		runtime.exec("C:/Dev-Cpp/pfe.exe");
    	} catch (IOException e) {
    		// TODO Auto-generated catch block
    		e.printStackTrace();
    			}
    }

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,299
    Blog Entries
    7
    Rep Power
    24

    Default Re: Run a c program through java

    That pfe.exe program, is it an interactive program, i.e. does it expect some input or does it produce output?

    kind regards,

    Jos
    The only person who got everything done by Friday was Robinson Crusoe.

  7. #7
    manou2324 is offline Member
    Join Date
    Mar 2013
    Posts
    56
    Rep Power
    0

    Default Re: Run a c program through java

    yes and i put the input files and that code and it runs correctly :
    Java Code:
    import java.lang.*;
    
    public class Pfe
    {
    	public static void main(String[] args) throws Exception
    	{
    		String[] env =  new String[] { "FOO_BAR=HelloWorld" };
    		Runtime.getRuntime().exec(new String[]{ "C:/Dev-Cpp/pfe.exe" }, env);
    	}
    }
    thanks

  8. #8
    cselic is offline Senior Member
    Join Date
    Apr 2010
    Location
    Belgrade, Serbia
    Posts
    278
    Rep Power
    7

    Default Re: Run a c program through java

    Is there any way in which we could include C/C++ code into java code?

  9. #9
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,299
    Blog Entries
    7
    Rep Power
    24

    Default Re: Run a c program through java

    Quote Originally Posted by cselic View Post
    Is there any way in which we could include C/C++ code into java code?
    What does 'include' mean?

    kind regards,

    Jos
    The only person who got everything done by Friday was Robinson Crusoe.

  10. #10
    cselic is offline Senior Member
    Join Date
    Apr 2010
    Location
    Belgrade, Serbia
    Posts
    278
    Rep Power
    7

    Default Re: Run a c program through java

    ^
    Java Code:
    // MyProgram.java
    
    package program;
    
    class MyProgram {
    
    
    // some C/C++ code here, or some C/C++ function called here, or some header file "myfunctions.h" 
    // included into MyProgram.java
    
    
    }

  11. #11
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,299
    Blog Entries
    7
    Rep Power
    24

    Default Re: Run a c program through java

    You can't mix C/C++ and Java source code; you can run compiled code from Java; see the rest of this thread for that. (you can't include Java source code in C/C++ source code either).

    kind regards,

    Jos
    The only person who got everything done by Friday was Robinson Crusoe.

Similar Threads

  1. Replies: 4
    Last Post: 11-21-2012, 02:19 PM
  2. Replies: 4
    Last Post: 07-30-2010, 12:25 PM
  3. execute java program within java program
    By popey in forum New To Java
    Replies: 2
    Last Post: 10-22-2009, 05:32 PM
  4. Replies: 0
    Last Post: 04-04-2008, 02:40 PM
  5. Replies: 0
    Last Post: 10-04-2007, 09:33 PM

Posting Permissions

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