Problem using java programs within C code

This is a discussion on Problem using java programs within C code within the Linux Programming forums, part of the Platform Specific Boards category; Hi guys, I am new to systems programming using C and on the Linux platform.i got a rather demoralizing problem, ...

  1. #1
    Registered User
    Join Date
    May 2005
    Posts
    1

    Problem using java programs within C code

    Hi guys, I am new to systems programming using C and on the Linux platform.i got a rather demoralizing problem, after developing my program over the weekend.

    I was tasked to develop an application which instructed me to use small java programs within my C code to call the small GUI programs using exepl. I have managed to code most of the code execpt after clicking on the OptionDialog( also a java based GUI program) i could not return to my main Listbox(java based Listbox)and select other files. Below is a general algorithm of how i used the pipe,fork and exepl commands.

    Aplogies if the code is too long or i hope some C systems programming expert could help me out. My guess is with the inner forks at processFileDesc_3 and processFileDesc_4.I have the full code and relevant java programs on hand.If anyone is interested in helping me out, drop me and email and i will send out the tar file.

    Code:
    main(int argc,char * argv[])
    
    {
    	int processFileDesc_1[2];
    	 
    	int processFileDesc_3[2];
    	int processFileDesc_4[2];
     
    
    	pipe(processFileDesc_1);
    
    	if(fork () != 0 )
    	{
    		//**************************************************************************
    		// Start of parent process processFileDesc_1
    		//**************************************************************************
    			
    			int processFileDesc_2[2];
    			pipe(processFileDesc_2);
    
    			if(fork()== 0)
    			{
    				//**********************************************************************
    				// Start of child process processFileDesc_2
    				//**********************************************************************
    				close(processFileDesc_2[1]);
    				dup2(processFileDesc_2[0],0);
    				close(processFileDesc_2[0]);
    			
    				char inputFile[MAXPATHLEN];
    			
    				while(scanf("%s",inputFile) > 0)
    				{	if(strstr(inputFile,".java")) // checks if the file is a java file
    					{
    						 
    						int processFileDesc_3[2];
    
    				    int processFileDesc_4[2];
    
    				    pipe(processFileDesc_3);
    
    				      
    
    				    if (fork() == 0)
    
    						{
    							//****************************************************************
    							//  start of child process processFileDesc_3
    							//****************************************************************
    
    					  	close(processFileDesc_3[1]);
    
    					  	dup2(processFileDesc_3[0],0);
    
    					  	close (processFileDesc_3[0]);
    
    					  	pipe(processFileDesc_4);
    
    					 
    				  		if(fork() == 0)
    
    					   	{
    								//**************************************************************
    								//  start of child process processFileDesc_4
    								//**************************************************************
    
    
    					     	char userInput[20];
    
    					     	close(processFileDesc_4[1]);
    
    					     	dup2(processFileDesc_4[0],0);
    
    					     	close(processFileDesc_4[0]);
    
    					      
    								while(scanf("%s",userInput) > 0)
    
    								{	
    								 
    
    					       	if(strstr(userInput,"Edit"))
    									{	
    										if(fork() == 0)
    										{					
    											printf("File to be opened is %s\n",inputFile);
    
    						
    											execlp("gedit","gedit",inputFile, NULL);
    										}
    
    						  		}
    
    					      	else if(strstr(userInput,"Compile"))
    									{
    
    										printf("File to be compiled is %s\n",inputFile);
    									 
    
    						
    
    								  }
    
    								}//end if while 
    								//**************************************************************
    								//  End of child process processFileDesc_4
    								//************************************************************** 
    
    							}//end if fork == 0 for processFileDesc_4
    
    					  	else
    
    					  	{
    								//**************************************************************
    								//  Start of parent process processFileDesc_4
    								//**************************************************************
    
    
    					     	close(processFileDesc_4[0]);
    
    					     	dup2(processFileDesc_4[1],1);
    
    					     	close(processFileDesc_4[1]);
    
    					     	execlp("java","java","ButtonSelect","Please make a choice", NULL);
    								
    								//**************************************************************
    								//  End of parent process processFileDesc_4
    								//**************************************************************
    
    					      
    
    					  	}//end else fork ==0 for processFileDesc_4
    
    						//******************************************************************
    						//  End of child process processFileDesc_3
    						//******************************************************************
    
    						}//end else fork ==0 for processFileDesc_3
    
    				    else
    
    						{
    							//****************************************************************
    							//  Start of parent process processFileDesc_3
    							//****************************************************************
    
    
    					  	close(processFileDesc_3[0]);
    
    					  	dup2(processFileDesc_3[1],1);
    
    					  	close(processFileDesc_3[1]);
    
    					  	execlp("echo","echo","-e","Edit\nCompile",NULL);
    
    							//****************************************************************
    							//  End of parnent process processFileDesc_3
    							//****************************************************************
    
    						}//end else fork ==0 for processFileDesc_3
    
    				 
    					}//end else (strstr == java)
    				 
    				}//end while 
    			}//end if
    
    			//************************************************************************
    			// End of child process processFileDesc_2
    			//************************************************************************
    			else
    			{
    				//**********************************************************************
    				// Start of parent process processFileDesc_2
    				//**********************************************************************
    					close(processFileDesc_2[0]);
    					dup2(processFileDesc_2[1],1);
    
    					close(processFileDesc_2[1]);
    
    					close (processFileDesc_1[1]);
    
    					dup2(processFileDesc_1[0],0);
    
    					close(processFileDesc_1[0]);
    
    					execlp("java","java","ListSelect","-persistent",NULL);
    
    				//**********************************************************************
    				// End of parent process processFileDesc_2
    				//**********************************************************************
    			}//end else 
    		//**************************************************************************
    		// End of parent process processFileDesc_1
    		//**************************************************************************
    	}//end if 
    	else
    	{
    		//**************************************************************************
    		// Start of child process processFileDesc_1
    		//**************************************************************************
     		 
    		printdir(toBeListed,processFileDesc_1[1]);
    		//**************************************************************************
    		// End of child process processFileDesc_1
    		//**************************************************************************
    
    	}//end else
    
    	
    }//end main

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,333
    Some general comments
    1. When posting code on a message board, set your text editor to use spaces for indentation (not tabs). Tabs get interpreted differently by every message board, and by every browser looking at that board. So for me, your code is a mess before anything else happens.

    2. All your comments are useless - remove them or add something better. At the moment, they're just bloating the code for no good reason.

    3. Don't use unreadable constants
    Code:
    #include <unistd.h>
    #define PIPE_RD 0
    #define PIPE_WR 1
    Then code like this
    dup2(processFileDesc_2[0],0);
    would be
    Code:
    dup2(processFileDesc_2[PIPE_RD],STDIN_FILENO);
    Which on the whole is more readable, and it enables you to make sense of which direction a particular pipe is meant to be going in.

    4. I'd suggest you use some functions to break up the code into more manageable blocks - it's over 200 lines long as it is, with 5 nested levels of fork()
    Say for example a function which just looks after edit/compile. With any luck, you should then be able to just test that function in isolation from all the other stuff you have going on.

    5.
    > int processFileDesc_3[2];
    > int processFileDesc_4[2];
    You have two lots of these, declared at different scopes.
    The outer ones are looking redundant.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with compiling code with option -O2
    By koushikyou in forum C Programming
    Replies: 16
    Last Post: 01-07-2009, 05:03 AM
  2. Problem with progress code
    By JFonseka in forum C Programming
    Replies: 13
    Last Post: 04-25-2008, 05:03 PM
  3. Problem with my morse code program
    By justin87 in forum C++ Programming
    Replies: 1
    Last Post: 10-21-2007, 05:23 PM
  4. code problem
    By maritos in forum C Programming
    Replies: 1
    Last Post: 11-27-2005, 04:22 PM
  5. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21