We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

Java's JNI Interface Problem

deepanp
deepanp asked
on
Medium Priority
280 Views
Last Modified: 2008-02-01
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
Comment
Watch Question

CERTIFIED EXPERT
Top Expert 2016
Commented:
>>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?

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
here are some examples that you should be able to easily modify for your needs

http://java.sun.com/developer/codesamples/jni.html

Author

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   ?
Mayank SPrincipal Technologist
CERTIFIED EXPERT

Commented:
>> 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 :)

Author

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 :)

Author

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.
Principal Technologist
CERTIFIED EXPERT
Commented:
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 () ;
Mayank SPrincipal Technologist
CERTIFIED EXPERT

Commented:
Anyway, if you are stuck on C, did you see the JNI tutorial?

Author

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?
CERTIFIED EXPERT
Top Expert 2016

Commented:
>>can it be used via a parallel port to drive a device like LCD

I think it's meant to support parallel ports

Author

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?
 
Mayank SPrincipal Technologist
CERTIFIED EXPERT

Commented:
>> 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 :) ?
Mayank SPrincipal Technologist
CERTIFIED EXPERT

Commented:
>> 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.

Author

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..

Author

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
Mayank SPrincipal Technologist
CERTIFIED EXPERT

Commented:
>> 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.

Author

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
Mayank SPrincipal Technologist
CERTIFIED EXPERT

Commented:
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.
CERTIFIED EXPERT
Top Expert 2016

Commented:
:-)

Author

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

 :-)
Mayank SPrincipal Technologist
CERTIFIED EXPERT

Commented:
Ok, does this deserve a B or an A ;-) ?
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.