• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 259
  • Last Modified:

Java's JNI Interface Problem

Hey to all the Experts, :-)

  -  I have a problem that needs solving, hope you can help me as I have been at it for some time and have not got anywhere.

  -  I have some java code that basically prints out some information, and then sends the information to a text file.

  - What I need is for the java program to call some C code, where the C Code reads the text file and then sends the data of the file to a port, say parallel.    

  -  Now the problem that I am basically having is understanding how I would exactly intergrate the C Code in the java program, so that the java program just calls the C function, so it can read              the text file and I think you get the rest of the picture....

;-)

Please provide some actual code!!
Or good examples of code from web, or some really good tutorials

Points will be awarded for the best responses

Thanks
Dipz
0
deepanp
Asked:
deepanp
  • 9
  • 8
  • 3
  • +1
2 Solutions
 
CEHJCommented:
>>where the C Code reads the text file and then sends the data of the file to a port, say parallel.

Why not just do that in Java?
0
 
objectsCommented:
here are some examples that you should be able to easily modify for your needs

http://java.sun.com/developer/codesamples/jni.html
0
 
deepanpAuthor Commented:
>>Why not just do that in Java?

>the reason for doing this in C is that, comfiguring the port, is much easier

>>thanks for the link objects..

Can any of you help in the coding bit though, :P   ?
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Mayank SAssociate Director - Product EngineeringCommented:
>> Now the problem that I am basically having is understanding how I would exactly intergrate the C Code in the java program

Let's first try to understand that problem. Is this a school/ college assignment? If yes, then you have to what you've been asked to :) if its about deciding which is the best way to read from the file, then of course you should go ahead with Java. It will be platform-independent code, and faster than calling a C function from Java (plus you don't have any other dependencies on the C function being available on any machine where you deploy your Java code). You can see this sample for reading from a file in Java:

http://www.javaalmanac.com/egs/java.io/WriteToFile.html

You can call C/ C++ functions from Java using JNI, but that should only be used for cases where Java cannot do for you what they do. If you want a tutorial, you can use:

http://java.sun.com/developer/onlineTraining/Programming/JDCBook/jni.html 

If you post some of your code, we should be able to set it right, hopefully :)
0
 
deepanpAuthor Commented:
import java.io.*;
import javax.xml.parsers.*;
import org.w3c.dom.*;
import org.xml.sax.*;
import java.io.IOException;
import java.net.URL;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
import java.util.*;
import java.awt.*;                  //lines 5 - 12 import java libraries
import java.awt.event.*;
import javax.swing.*;
import java.net.*;
import java.io.*;
import java.util.*;
import javax.servlet.*;            //import java Libraries
import javax.servlet.http.*;


public class firstdom extends HttpServlet
{
      public void doGet (HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException      //doGet method
{
            response.setContentType ("text/html");            //sets response to content type - html
          PrintWriter out = response.getWriter();            //creates a printwriter out for reponse.
          
          try
{
           
String xml = request.getParameter ("xmlurl");      //gets parameters and stores them into the String fields

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = factory.newDocumentBuilder();
Document document = docBuilder.parse (xml);

NodeList channel = document.getElementsByTagName ("channel");
NodeList item = document.getElementsByTagName ("item");
NodeList title = document.getElementsByTagName ("title");
NodeList des = document.getElementsByTagName ("description");
NodeList link = document.getElementsByTagName ("link");
NodeList guidv = document.getElementsByTagName ("isPermaLink");
NodeList guid = document.getElementsByTagName ("guid");
NodeList pubdate = document.getElementsByTagName ("pubDate");
NodeList category = document.getElementsByTagName ("category");
NodeList url = document.getElementsByTagName ("url");
NodeList language = document.getElementsByTagName ("language");
NodeList lastbuilddate = document.getElementsByTagName ("lastBuildDate");
NodeList copyright = document.getElementsByTagName ("copyright");
NodeList ttl = document.getElementsByTagName ("ttl");
NodeList image = document.getElementsByTagName ("image");

BufferedWriter outfile = new BufferedWriter (new FileWriter ("C:/Documents and Settings/Deepan Patel/Desktop/CXML.txt"));
//out.println(link.getLength());

      for (int i = 0; i < image.getLength(); i++)
{
       out.println("**********THIS IS THE IMAGE ELEMENT**********<br>");
       out.println("<br>**********Title**********<br>");
       out.println(((Element)title.item(1)).getFirstChild().getNodeValue());
       outfile.write((((Element)title.item(1)).getFirstChild().getNodeValue()));
       outfile.write("\n");
       out.println("<br><br>**********URL**********<br>");
       out.println(((Element)url.item(0)).getFirstChild().getNodeValue());
       out.println("<br><br>**********Link**********<br>");
       out.println(((Element)link.item(1)).getFirstChild().getNodeValue());
       out.println("<br><br>**********THIS IS THE END OF IMAGE ELEMENT**********<br>");
}

      for (int i = 0; i < channel.getLength(); i++)
     
{
       out.println("<br><br>**********ELEMENTS THAT LAY OUTSIDE ITEM & IMAGE ELEMENT**********");
       out.println("<br><br>**********Title**********<br>");
       out.println(((Element)title.item(i)).getFirstChild().getNodeValue());
       outfile.write("\n");
       outfile.write((((Element)title.item(i)).getFirstChild().getNodeValue()));
       outfile.write("\n");
       out.println("<br><br>**********Description**********<br>");
     out.println(((Element)des.item(i)).getFirstChild().getNodeValue());
     outfile.write((((Element)des.item(i)).getFirstChild().getNodeValue()));
     outfile.write("\n");
       out.println("<br><br>**********Link**********<br>");
       out.println(((Element)link.item(i)).getFirstChild().getNodeValue());
       out.println("<br><br>**********Language**********<br>");
       out.println(((Element)language.item(i)).getFirstChild().getNodeValue());
       out.println("<br><br>**********Last Build Build**********<br>");
       out.println(((Element)lastbuilddate.item(i)).getFirstChild().getNodeValue());
       out.println("<br><br>**********Copyright**********<br>");
       out.println(((Element)copyright.item(i)).getFirstChild().getNodeValue());
       out.println("<br><br>**********TTL**********<br>");
       out.println(((Element)ttl.item(i)).getFirstChild().getNodeValue());
     out.println("<br><br>**********THIS IS THE END OF THE ELEMENTS IN ELEMENT CHANNEL**********<br>");
}

      out.println("<br><br>**********ITEM ELEMENTS BEGIN HERE**********<br>");

      int a = 1;
      int b = 0;
      for (int i = 2; i < item.getLength(); i ++)
{
      out.println ("<br><br>**********Title**********<br>");
      out.println (((Element)title.item(i)).getFirstChild().getNodeValue());
      outfile.write("\n");
      outfile.write((((Element)title.item(i)).getFirstChild().getNodeValue()));
      out.println ("<br><br>**********Description**********<br>");
      out.println (((Element)des.item(a)).getFirstChild().getNodeValue());
      outfile.write("\n");
      outfile.write((((Element)des.item(i)).getFirstChild().getNodeValue()));
      outfile.write("\n");
      out.println ("<br><br>**********Link**********<br>");
      out.println (((Element)link.item(i)).getFirstChild().getNodeValue());
      out.println ("<br><br>**********Guid**********<br>");
      out.println (((Element)guid.item(b)).getFirstChild().getNodeValue());
      out.println ("<br><br> isPermaLink =  ");
      out.println (((Element)guid.item(b)).getAttribute("isPermaLink"));
      out.println ("<br><br>**********Pub-Date**********<br>");
      out.println (((Element)pubdate.item(i)).getFirstChild().getNodeValue());
      out.println ("<br><br>**********Category**********<br>");
      out.println (((Element)category.item(b)).getFirstChild().getNodeValue());
      a++;
      b++;
}
      int d = 59;
      int e = 58;
      for (int c = 60;c <= 61;c++)
{
      out.println ("<br><br>**********Title**********<br>");
      out.println (((Element)title.item(c)).getFirstChild().getNodeValue());
      outfile.write("\n");
      outfile.write((((Element)title.item(c)).getFirstChild().getNodeValue()));
      out.println ("<br><br>**********Description**********<br>");
      out.println (((Element)des.item(d)).getFirstChild().getNodeValue());
      outfile.write("\n");
      outfile.write((((Element)des.item(d)).getFirstChild().getNodeValue()));
      outfile.write("\n");
      out.println ("<br><br>**********Link**********<br>");
      out.println (((Element)link.item(c)).getFirstChild().getNodeValue());
      out.println ("<br><br>**********Guid**********<br>");
      out.println (((Element)guid.item(e)).getFirstChild().getNodeValue());
      out.println ("<br><br> isPermaLink =  ");
      out.println (((Element)guid.item(e)).getAttribute("isPermaLink"));
      out.println ("<br><br>**********Pub-Date**********<br>");
      out.println (((Element)pubdate.item(e)).getFirstChild().getNodeValue());
      out.println ("<br><br>**********Category**********<br>");
      out.println (((Element)category.item(e)).getFirstChild().getNodeValue());
      d++;
      e++;
}
outfile.close();
      out.println ("<br><br>**********ITEM ELEMENTS ENDS HERE**********<br>");
}
          catch (SAXException e)
{
                // A parsing error occurred; the xml input is not valid
}
      catch (ParserConfigurationException e)
{
}
      catch (IOException e)
{
      
}

}
}

class seconddom

{
     public static void main(String s[])
{
          firstdom f = new firstdom();
}
}

basically this code gets all the elements from xml file right, i have cracked that bit now :-), thanks to the help of experts... Now the information should be sent to a hardware device, and the problem that i have is that using the java comms api, i heard that it is alot easier doing it in C, not to mention, there are alot more examples of code that does the function which are in C.

Which is hence, why i need to figure out how to call the C code from java. I hope this helps.

Thanks Experts :)
0
 
deepanpAuthor Commented:
Sorry, missed a bit, the reason why i have a line that rights the information to a text file is because C can then read that information, which causes less complications, rather than making the two communicate with parameters and so on, the java code simply calls the C code, so that the C code reads the text file and sends information to a device.
0
 
Mayank SAssociate Director - Product EngineeringCommented:
Actually, using Java Comm is easier, I guess because you have better abstraction of methods and the whole thing might be achieved in lesser lines of code:

String s = readFromFileInJava () ;
CommPortIdentifier id = CommPortIdentifier.getPortIdentifier ( "COM port number" ) ;
CommPort port = id.open ( "MyApplication", 1000 ) ;
OutputStream s = port.getOutputStream () ;
s.write ( s.getBytes () ) ;
s.flush () ;
s.close () ;
0
 
Mayank SAssociate Director - Product EngineeringCommented:
Anyway, if you are stuck on C, did you see the JNI tutorial?
0
 
deepanpAuthor Commented:
Yeh, since i haven't gt a clue how to do C, since i hve dne it years ago, i was having trouble, and for the java comms, if it is easier, and it looks easier, can it be used via a parallel port to drive a device like LCD, that displays information.

I was told that the parallel port is tough to configure using windows xp, because windows doesn't allow you to send data like dt..

Do u knw if this is true?
0
 
CEHJCommented:
>>can it be used via a parallel port to drive a device like LCD

I think it's meant to support parallel ports
0
 
deepanpAuthor Commented:
hmmm, thats werid.. i think i saw on the net that it supports only some functionlity of parallel but mostly serial...hmmm..

If thats the case, serial should be okay, since i think we have a working serial LCD device as well as parallel..

I will have to try it today.

:-)..

For the code CEHJ,

instead of readin a text file now, do u think its easier to just send the string of the element, i.e.
String s = (((Element)title.item(1)).getFirstChild().getNodeValue());

Or is it easier to read the text file, not sure which has better benifit..

Also can someone explain what this line means --
CommPortIdentifier id = CommPortIdentifier.getPortIdentifier ( "COM port number" ) ;    /// if i'm not right, correct me, but is this the port number of serial port, i.e. port 12
CommPort port = id.open ( "MyApplication", 1000 ) ;   // i dunno what this means, any help please?
 
0
 
Mayank SAssociate Director - Product EngineeringCommented:
>> I was told that the parallel port is tough to configure using windows xp, because windows doesn't allow you to send data like dt

Then how do you think normal desktop printers work :) ?
0
 
Mayank SAssociate Director - Product EngineeringCommented:
>> Or is it easier to read the text file, not sure which has better benifit..

Depends on what you want to send to the port (whatever the thing on the other side understands).

>> if i'm not right, correct me, but is this the port number of serial port, i.e. port 12

Yes the number. For example, if its COM port 1, then "COM1".

>> // i dunno what this means, any help please

The name of the application (your application) which is reserving the port, and the time-out (in milliseconds) for connecting to the port.
0
 
deepanpAuthor Commented:
haha, thats true, i'm not exactly sure, but i have been moving back and forwards with the JNI and Java Comms, some third year people said that its better doing it through C and using JNI, but in my head i feel that java comms api looks easier, lol...

And i have been on java for like 4 weeks straight, CEHJ probably knws dt, lol..
0
 
deepanpAuthor Commented:
>>Depends on what you want to send to the port (whatever the thing on the other side understands).

its basically meant to just display text on the LCD, and the thing on the other side understands, hmm... i am not sure what u mean by that, do u mean the LCD interface, (i'd think so)?

>>The name of the application (your application) which is reserving the port, and the time-out (in milliseconds) for connecting to the port.

Brilliant
0
 
Mayank SAssociate Director - Product EngineeringCommented:
>> its basically meant to just display text on the LCD

All the text that's sent on the port can be displayed on it? Well, then its your choice :) whatever you think can be displayed. What were you planning to display using the C program? Do the same. I guess the whole file will look messy, so try just the node-names in a manner that makes sense.
0
 
deepanpAuthor Commented:
but is java comms good for this type of method then?

Thats what i am not sure about, i think your right, doing it in C might be messy, but i need to be certain if i do it in java comms, isit been proven to be used for LCd things, thats wt i dnt know
0
 
Mayank SAssociate Director - Product EngineeringCommented:
For serial ports, its good. For parallel, I'm not too sure, but CEHJ says it probably is. Also, I know that it has a javax.comm.ParallelPort class so I guess the support should be there. I have used it for serial ports and not faced any problems.
0
 
CEHJCommented:
:-)
0
 
deepanpAuthor Commented:
It was time to clean this topic up, as there is another topic that i will probably be bringin out soon, as i am having trouble with some hardware issues (dunno wheter anyone would be able to help with that)..

Thanks for all the help that you have given and will continue to give

 :-)
0
 
Mayank SAssociate Director - Product EngineeringCommented:
Ok, does this deserve a B or an A ;-) ?
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 9
  • 8
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now