问题
Is there a way to read cobol data in a Java program? More concretely I'm confronted with the following case:
I have a file with fixed length records of data. The data definition is done as Cobol copybooks. What I think of is a library which taking into account the copybooks would be able to read those records.
Ideally it should be possible to generate basic java classes and structures based on the copybook information. In a later step the datarecords would be parsed and the data filled into objects of those generated classes.
Do you know a library providing this kind of functionality? Or any other techniques to cope with the problem of reading cobol data?
There are some commercial tools which provide this functionality. They are, however, very expensive. Do you know any open-source alternative or a combination of different tools to provide the entire functionality? For example the cb2xml reads copybook structures and transforms them into xml. A later step would be to generate a record reader based on the cb2xml xml information. Or to use a record reader which is configurable. Do you know a cobol record reader (decoder)?
回答1:
You could look at JRecord - http://sourceforge.net/projects/jrecord/ or cb2java http://sourceforge.net/projects/cb2java/ both allow you to access cobol files but neither will generate the full classes.
Update Jan 2011
Since the original Answer:
- JRecord continues be developed. There is now a
JRecord Code generator
available as either a standalone program or in the Recordeditor. ThisCode Generator
will buildJRecord JRecord
code from a Cobol Copybook. See RecordEditor Jrecord CodeGen - Development on cb2java has stopped
- cobol2j has been written. There have been no updates for a year.
- There is also Legstar again nothing published for a few years
Update Aug 2017
The RecordEditor has a Generate option for generating Java / JRecord code from a Cobol Copybook. See RecordEditor Code Generation notes for details.
Update Jan 2018
There is some information on generating Java~JRecord code in this question / answer:
How do you generate java~jrecord code for a Cobol copybook
回答2:
Yes. I have done that before. I used an ODBC connection to COBOL files, and then with jdbc:odbc bridge, I used metadata information to generate classes, read data and port it all to Oracle.
Here is a nice tutorial on how to access metada information with JDBC. Here is another one.
Keep in mind that you don't need the JDBC:ODBC bridge approach. If you can get a native JDBC driver to connect to your Cobol DataSource, it will be better. In this regard, I also used an IBM native driver. Don't remember the name though. It was a long time ago.
回答3:
There appear to be some commercial solutions for this. Alternatively you can use cb2xml to convert the copybooks to XML, and then import the XML into Java using whatever mechanism you require.
回答4:
I have used Bruce's JRecord (from sourceforge) package for my project. It took only couple of days to learn to use it and saved me months of work in rolling out a much less general solution on my own. I recommend it highly.
回答5:
Microfocus provide a way of calling OO COBOL from Java.
"You can write classes in OO COBOL which can be called from Java programs as though they were Java classes. You do this by providing a Java wrapper class, which provides a function for each method in the OO COBOL class. The Net Express Class and Method Wizards make this easy for you, by generating the Java code at the same time as the COBOL code."
They also provide a tool called Enterprise Server which allows COBOL to interact with web services.
If you have a COBOL program A, the tool allows you to expose A's interface section as a web service.
Of course, because A now has a web service any other type of program (command line, Windows application, Java, ASP etc.) can now also call it and hence pass the COBOL data across to a Java program.
回答6:
BEA used to have a product named JAM that was used to communicate with mainframe COBOL programs. It included a tool that would read copybooks and generate both corresponding Java POD classes and data conversion code.
I don't know if this is still available, I lost track of it when I left BEA.
回答7:
Rational Application Developer can read COBOL source code and generate Java classes. The generated classes have methods for accessing the various part of the COBOL data structure. The class that is generated is compatible with the J2EE Connector Architecture. To create a class in your project, select File, New, Other then select the CICS/IMS Java Data Binding wizard under J2C. Click next. Choose COBOL to Java for mapping. Select your COBOL file. Select the structure you wish to generate a Java class for then click Finish and there you go. There are of course a number of options you can select along the way that I didn't mention. For more information search Help for J2C.
回答8:
Have a look at Javolution Struct.
You can then use a macro to convert your COBOL datat into Struct
.
来源:https://stackoverflow.com/questions/802122/reading-cobol-datastructures-from-java