C programmer - low level coding in Java?

D

Dev_Prog

Hello all,
2 quick questions re low level programming in Java:
q1. Can one talk with the PC hardware from inside the Java code
(standalone application)? I mean i/o with ports.
q2. Does Java allow programmers to write some pieces of the code in C
or assembly? I mean inline of linkable (.obj) files.
If you're interested in the reason for my questions, then it's simply
coming from my current study project under Linux. Java sounds to be my
preferred programming language for the future, coming from C. My
project needs to read from a home-made PC card and show the readings
graphically. I had a successful previous experience in drawing in
Java, but interacting with the hardware is a new ground for me.
One last word, cross platform programming in definitely one of Java's
strong capabilities, but it's not important in this current instance.
My target is Linux. So feel welcome to propose solutions that are not
portable.
Thanks all for a quick reply.
 
S

Sudsy

Dev_Prog said:
Hello all,
2 quick questions re low level programming in Java:

The answer to both questions is the same: use JNI (Java Native
Interface). The low-level, platform-dependent stuff is written
in C and linked into a dynamic library which is loaded by your
Java code. The best of both worlds and a prime example of what
I promote about "using the right tool for the job" (naysayers
notwithstanding).
Roedy has some resources on how to get started and Marco might
have some good readings as well. Search the archives to find
links.
 
N

Nathan

Hello all,
2 quick questions re low level programming in Java:
q1. Can one talk with the PC hardware from inside the Java code
(standalone application)? I mean i/o with ports.
q2. Does Java allow programmers to write some pieces of the code in C
or assembly? I mean inline of linkable (.obj) files.
If you're interested in the reason for my questions, then it's simply
coming from my current study project under Linux. Java sounds to be my
preferred programming language for the future, coming from C. My
project needs to read from a home-made PC card and show the readings
graphically. I had a successful previous experience in drawing in
Java, but interacting with the hardware is a new ground for me.
One last word, cross platform programming in definitely one of Java's
strong capabilities, but it's not important in this current instance.
My target is Linux. So feel welcome to propose solutions that are not
portable.
Thanks all for a quick reply.

You can use the JNI, Java Native Interface. With the JNI you can call
your native C functions from your Java code.

On the Sun site you can find a tutorial on using the JNI.
http://java.sun.com/docs/books/tutorial/native1.1/

If you want to use assembly also and you also want to have some kind
of portability, you could consider to define a layer of C functions
between the assembly code and the Java code. So the Java code calls
the native C functions, these then call the assembly routines.

Regards,
Nathan
 
A

Anthony Borla

Dev_Prog said:
Hello all,
2 quick questions re low level programming in Java:
q1. Can one talk with the PC hardware from inside the
Java code (standalone application)? I mean i/o with ports.

Two ways, neither one direct:

* javax.comm package
* JNI - see below

Note access to ports is operating system dependant. Usually the OS provides
system calls granting such access, and you Java code calls these to perform
hardware interfacing.
q2. Does Java allow programmers to write some pieces of
the code in C or assembly? I mean inline of linkable (.obj) files.

JNI - Java Native Interface. Allows packaging of native C / C++ functions
inside *NIX shared libraries [Win32 DLL's]. Some work involved in creating
appropriate Java wrapper functions, but definitely possible.
If you're interested in the reason for my questions, then it's simply
coming from my current study project under Linux. Java sounds
to be my preferred programming language for the future, coming
from C. My project needs to read from a home-made PC card
and show the readings graphically. I had a successful previous
experience in drawing in Java, but interacting with the hardware
is a new ground for me.

One last word, cross platform programming in definitely one
of Java's strong capabilities, but it's not important in this current
instance. My target is Linux. So feel welcome to propose solutions
that are not portable.

Stick to C for low-level work:

* It's your area of expertise
* It's small, lean, fast, efficient
* There's *plenty* of code out there both commercial,
and public domain / GPL etc.

Also [and it actually depnds on what you need to do], you can do your
graphics in C, Java, or a suitable scripting language. Again, *lots* of C
libraries out there which many *other* languages [not Java, of course :)]
seem to use.

Don't misunderstand me - Java is terrific, offering many benefits. However,
given your circumstances, and the non-urgency in requiring platform
independance, your project could be impeded by you tackling a new language
when, strictly speaking, you not need to. Food for thought, anyway :)
Thanks all for a quick reply.

I hope this helps.

Anthony Borla
 
D

Dev_Prog

Thanks all for your contributions.
JNI is a new ground for me so I'll start digging into that tonight.
Heaps of thanks to Anthony for your comments. I agree with you but I
do not feel comfortable re GUI programming in Linux. X Window system
sounds a bit too primitive for me. Research & readings suggested using
Qt or Xt libraries, but I preferred to take this opportunity to make
my hands dirty with Java rather than learning Linux GUI technologies
that might take you nowhere.
Thanks again to all, and wish you a very merry Christmas.
Cheers.
 
S

Sudsy

Dev_Prog said:
Thanks all for your contributions.
JNI is a new ground for me so I'll start digging into that tonight.
Heaps of thanks to Anthony for your comments. I agree with you but I
do not feel comfortable re GUI programming in Linux. X Window system
sounds a bit too primitive for me. Research & readings suggested using
Qt or Xt libraries, but I preferred to take this opportunity to make
my hands dirty with Java rather than learning Linux GUI technologies
that might take you nowhere.
Thanks again to all, and wish you a very merry Christmas.
Cheers.

An interesting observation about X windows. Rather than being a
dead-end it remains a viable option and is, in many cases, more
powerful than AWT/Swing. But then it's been around a lot longer
too...
I'd tend towards developing the GUI in Java just for the sake of
portability. But there are some things you can do in C which are
essentially impossible from the Java level, hence the need for
JNI.

ps. I don't believe that X11/Motif programming is more difficult
(or arcane) than AWT/Swing, just different.
difficult
 
C

Cyrille \cns\ Szymanski

ps. I don't believe that X11/Motif programming is more difficult
(or arcane) than AWT/Swing, just different.
difficult

Difficulty is indeed relative to the programmer's knowledge of the tools
he's using, so if you need things to be done fast, i'd say you'd rather use
what you know.
 
S

Sudsy

Cyrille said:
Difficulty is indeed relative to the programmer's knowledge of the tools
he's using, so if you need things to be done fast, i'd say you'd rather use
what you know.

Fair enough. I was using X11/Motif for years so it was fairly easy
to transition to the AWT. Event-driven programming was already second
nature by that point. It certainly CAN be somewhat daunting to try to
pick it up going the other way. There are a lot of very fat books and
no javadocs ;-)
 
J

Jon A. Cruz

Sudsy said:
Fair enough. I was using X11/Motif for years so it was fairly easy
to transition to the AWT.

But remember, there's X11/GTK+, X11/Qt and many others.

:)

But very few people seem to code to X windows directly.

Oh, and "AWT/Swing" is a little more comparable to saying "GTK+/GTK+2"
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,139
Latest member
JamaalCald
Top