Does anyone know wether i can convert a Java written program to C?

Thank You

Dani AI

Generated

For : short answer — it is possible in principle, but not a simple source-to-source conversion and rarely worth the effort for anything nontrivial. Java relies on a managed runtime (garbage collection, a rich standard library, reflection, dynamic classloading, threads and well-defined object semantics). Reproducing all of that correctly in plain C is time consuming and error prone.

As hinted, there are better practical routes than trying to translate every Java file to C. Common choices today are:

  • Produce a native executable with an ahead-of-time compiler (no C step) for a standalone binary.
  • Keep most code in Java and rewrite only hot or system-level modules in C, calling them via JNI or a foreign-function interface.
  • For a full rewrite, port the design rather than attempt automatic line-by-line translation.

If a manual port is chosen, follow a checklist:

  • Decide why you need C (size, performance, embedability).
  • Inventory Java features used (reflection, dynamic loading, third-party libraries).
  • Map classes to C structs and methods to functions; plan memory management (manual or link a conservative GC).
  • Replace Java collections and String APIs with appropriate C libraries.
  • Translate exceptions (use return codes or setjmp/longjmp) and map threading to pthreads.
  • Keep a comprehensive test suite and port incrementally.

Small example of mapping a trivial class to C:

/* Java
class Counter { private int c; public void inc(){c++;} public int get(){return c;} }
*/

/* C */
typedef struct { int c; } Counter;
void Counter_init(Counter *s) { s->c = 0; }
void Counter_inc(Counter *s) { s->c++; }
int Counter_get(Counter *s) { return s->c; }

Recommendation: profile first. If the goal is performance or a standalone binary, prototype with an AOT/native-image approach or isolate and rewrite the real hotspots in C. Full automatic conversion is uncommon and creates long-term maintenance headaches.

Recommended Answers

All 2 Replies

There is some sotware called Toba here:

There is some sotware called Toba here:

But I guess there wasn't much call for it, as it isn't supported and only works for Java 1.1.

If you are looking to compile Java to native code you can do that directly without the C step. Which also can be found via the above link or just Google a bit.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.