This guide is based on our blog post Refactoring COBOL to Java with AI Agents.
The COBOL Modernization Challenge
COBOL modernization is one of the most pressing challenges facing enterprises today. Gartner estimated there were over 200 billion lines of COBOL code in existence, running 80% of the world’s business systems. As of 2020, COBOL was still running background processes for 95% of credit and debit card transactions. The challenge is acute: 47% of organizations struggle to fill COBOL roles, with salaries rising 25% annually. By 2027, 92% of remaining COBOL developers will have retired. Traditional modernization approaches have seen high failure rates, with COBOL’s specialized nature requiring a unique skill set that makes it difficult for human teams alone.Overview
COBOL modernization is a complex undertaking. Every modernization effort is unique and requires careful planning, execution, and validation to ensure the modernized code behaves identically to the original. The migration needs to be driven by an experienced team of developers and domain experts, but even that isn’t sufficient to ensure the job is done quickly or cost-effectively. This is where OpenHands comes in. OpenHands is a powerful agent that assists in modernizing COBOL code along every step of the process:- Understanding: Analyze and document existing COBOL code
- Translation: Convert COBOL to modern languages like Java, Python, or C#
- Validation: Ensure the modernized code behaves identically to the original
Understanding
A significant challenge in modernization is understanding the business function of the code. Developers have practice determining the “how” of the code, even in legacy systems with unfamiliar syntax and keywords, but understanding the “why” is more important to ensure that business logic is preserved accurately. The difficulty then comes from the fact that business function is only implicitly represented in the code and requires external documentation or domain expertise to untangle. Fortunately, agents like OpenHands are able to understand source code and process-oriented documentation, and this simultaneous view lets them link the two together in a way that makes every downstream process more transparent and predictable. Your COBOL source might already have some structure or comments that make this link clear, but if not OpenHands can help. If your COBOL source is in/src and your process-oriented documentation is in /docs, the following prompt will establish a link between the two and save it for future reference:
grep, sed, and awk to navigate files and pull in context. This is natural for source code and also works well for process-oriented documentation, but in some cases exposing the latter using a semantic search engine instead will yield better results. Semantic search engines can understand the meaning behind words and phrases, making it easier to find relevant information.
Translation
With a clear picture of what each program does and why, the next step is translating the COBOL source into your target language. The example prompts in this section target Java, but the same approach works for Python, C#, or any modern language. Just adjust for language-specific idioms and data types as needed. One thing to watch out for: COBOL keywords and data types do not always match one-to-one with their Java counterparts. For example, COBOL’s decimal data type (PIC S9(9)V9(9)), which represents a fixed-point number with a scale of 9 digits, does not have a direct equivalent in Java. Instead, you might use BigDecimal with a scale of 9, but be aware of potential precision issues when converting between the two. A solid test suite will help catch these corner cases but including such known problems in the translation prompt can help prevent such errors from being introduced at all.
An example prompt is below:
Validation
Building confidence in the migrated code is crucial. Ideally, existing end-to-end tests can be reused to validate that business logic has been preserved. If you need to strengthen the testing setup, consider golden file testing. This involves capturing the COBOL program’s outputs for a set of known inputs, then verifying the translated code produces identical results. When generating inputs, pay particular attention to decimal precision in monetary calculations (COBOL’s fixed-point arithmetic doesn’t always map cleanly to Java’s BigDecimal) and date handling, where COBOL’s conventions can diverge from modern defaults. Every modernization effort is unique, and developer experience is crucial to ensure the testing strategy covers your organization’s requirements. Best practices still apply. A solid test suite will not only ensure the migrated code works as expected, but will also help the translation agent converge to a high-quality solution. Of course, OpenHands can help migrate tests, ensure they run and test the migrated code correctly, and even generate new tests to cover edge cases.Scaling Up
The largest challenge in scaling modernization efforts is dealing with agents’ limited attention span. Asking a single agent to handle the entire migration process in one go will almost certainly lead to errors and low-quality code as the context window is filled and flushed again and again. One way to address this is by tying translation and validation together in an iterative refinement loop. The idea is straightforward: one agent migrates some amount of code, and another agent critiques the migration. If the quality doesn’t meet the standards of the critic, the first agent is given some actionable feedback and the process repeats. Here’s what that looks like using the OpenHands SDK:critique.json and be prompted to act on the feedback.
This iterative refinement pattern works well for medium-sized projects with a moderate level of complexity. For legacy systems that span hundreds of files, however, the migration and critique processes need to be further decomposed to prevent agents from being overwhelmed. A natural way to do so is to break the system into smaller components, each with its own migration and critique processes. This process can be automated by using the OpenHands large codebase SDK, which combines agentic intelligence with static analysis tools to decompose large projects and orchestrate parallel agents in a dependency-aware manner.
Try It Yourself
The full iterative refinement example is available in the OpenHands SDK:Related Resources
- OpenHands SDK Repository - Build custom AI agents
- AWS CardDemo Application - Sample COBOL application for testing
- Prompting Best Practices - Write effective prompts

