Skip to main content
gamedev.net
🔒 Locked

[java] Running C Code in java

Started by ug02070 Jan 23, 2006 at 7:29 AM 10 replies 1.9k views
Original Post
ug02070
ug02070
hi, i was meant to make a script to automate tasks that needs running by some c code, but as i don't know much about scripting i ahve done it in java instead. i have all the prelim work done and i now just need to call the c code and pass it some arguments. can someone please tell me how i make the .c code so that i ahve an exe that i can call from java plz thanks in advance Danny =)
rpg_code_master
rpg_code_master
Try creating a normal C program then compiling it into an .exe program. Make that program accept run time arguments (via the main function), then call that program will the Java equivalent of system( "MyCProgram.exe -arg1 -arg2" );

I hope this helps :)
ug02070
ug02070
sorry but how do i make it into an exe? i dont know c so i ahven't programmed the c code i just need to run it as i know what arguments it takes
thanks
someboddy
someboddy
Click here
-----------------------------------------Everyboddy need someboddy!
ug02070
ug02070
no, i don't want that!! gonna try mingw
Son of Cain
Son of Cain
Uh?

// C code entry pointint main(char* argv[]) {    // your task code}


Then, call it with this (1.5 stuff).

Son Of Cain
a.k.a javabeats at yahoo.ca
ug02070
ug02070
ok, i made the exe using mingw and i was ablt to run the code by going to the directory where the exe's are stored and using the command train_sarray myFile.txt
and so i turned back to my java and created the following:

public static void train_sarray(String cmd)
{
try
{
Runtime rt = Runtime.getRuntime();
cmd = "C:\\Documents and Settings\\Danny\\Desktop\\sarray_exe\\train_sarray myFile.txt";

Process proc = rt.exec(cmd);
}
catch(IOException e)
{
System.out.println("Exception occurred...exiting");
System.exit(0);
}
System.out.println("Successful");
}

but the exception is thrown, does anyone know why? obviously i am missing something.
thanks
Whackjack
Whackjack
Try adding e.printStackTrace() to your catch block. An exception could be thrown for any number of reasons. The stack trace will help.
ug02070
ug02070
i sorted it, but now i have a new problem =(
the c code is executed by train_sarray 13 t.sarray
and i thought that t.sarray were arguments but they are not, it is sending the output from t.string being passed to the process with the output being stored in t.sarray and i really don't know how to do this in java, i guess i will need a lot of bufferedreaders and writers or something =(
ug02070
ug02070
ok, still trying to overcome the t.sarray, god i wish i had learned bash now, at least unix recognises commands to do this.

anyway, i have programmed the following but have now ran out of ideas:

public static void train_sarray(String[] cmdline) throws IOException{	String line;	Process p = Runtime.getRuntime().exec(new String[] {"C:\\sarray_exe\\train_sarray.exe", "-a", "13"});	        boolean processRunning = true;			     	String inputFile = "C:\\sarray_exe\\T2.string";	FileReader reader = new FileReader(inputFile);	BufferedReader in = new BufferedReader(reader);			     	FileWriter writer = new FileWriter("C:\\sarray_exe\\t2.sarray");			     	BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));			     	String line2;			     	while(processRunning)	{	 line = in.readLine();	 if(line != null)	 {		 System.out.println("Write to outputStream");		 System.out.println(line); // Output seems fine	         //write to outputStream	 }                 // SYSTEM HANGS HERE!!!!!!!!!!!	    	 line2 = input.readLine();	    	 if(line2 != null)	    	 {	        	 System.out.println("Write to fileWriter");			 //write to fileWriter			 writer.write(line2);		 }			    	 		 try		 {			int i = p.exitValue();			System.out.println(i);			processRunning = false;		 }		 catch(IllegalThreadStateException e)	         {			// Carry On    		 		 }}


Is anyone able to help, anything will be appreciated
thanks
SteveTaylor
SteveTaylor
Another way to mix C and Java is to use gcc and gcj. If you don't use that, but you want to make an exe from Java, try JSmooth.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.