1

I have a C# code snippet where I read a very old MS Access Database. It is a MS-Access 95 Database, which is not supported in Java's UCanAccess lib. So I had to switch to C# and retrieve the data from here. The process done in C# should be called after specific actions happen, which are done in Java. So I want to call the C# method from Java, if there is a possibility. The database gets updated every now and then because it is connected to an equipment which writes its data to this access database.

Is there a way to call the C' method from Java? Or do you have any other ideas on how to do it?

NECben067
  • 427
  • 1
  • 4
  • 20
  • 3
    This seems like an X-Y problem, because from your description what you really want is a way to read MS Access 95 files from Java. – ckuri Sep 24 '20 at 07:21
  • @VimalCK I believe that it's surely could be solved by building a dll. But this is a way harder to implement than a simple console app. – Andrei Prigorshnev Sep 24 '20 at 08:05
  • @AndrewPrigorshnev creating and consuming a dll is the simplest solution. Otherwise he should be ready to upgrade his access database to support the java library he use. And if still he cannot do that then write a program in any language to fetch data from access and create an xml or json file in a specific location. His java application should implement a filewatcher mechanism and process the file. – Vimal CK Sep 24 '20 at 08:11
  • @VimalCK Why don't consume data directly from a console application output instead of saving it to file and watching this file on the other side? – Andrei Prigorshnev Sep 24 '20 at 08:21
  • @AndrewPrigorshnev As per my understanding, java application will not be able to fetch data directly from c# console. and if you still need, you have to do marshaling. And even it is possible, you have to process 1000 lines and format according to your need. a xml or json file is easy to process in java with libraries – Vimal CK Sep 24 '20 at 08:40
  • Just use a COM object, Java can work with those. And don't involve C# at all, Access comes with ADO and DAO, which offer a comprehensive set of COM objects to work with databases (but if you do want communication between C# and Java, COM objects are the way to do it) – Erik A Sep 24 '20 at 09:04
  • @VimalCK There is no such thing as a C# console when we're talking about building console applications. When you build a console application using C# or any other language, you just build a usual console application for your operating system. For example, `git` is a console operation. When your IDE performs git operations, it just calls a git console application under the hood. In the same way, you can call git console application from your Java program. And in the same way, you can write your own console application (using C# or whatever) and call it from your Java program. – Andrei Prigorshnev Sep 24 '20 at 09:18
  • I have mentioned about C# console because the question is about how to get data from C# to Java. and console application building on different language runs on different environment. Eg: C# console in CLR, Java Console in JVM. All these runtime use common operating system console. eg: Windows it is Powershell or MS Console. So the question is how to access data from one application to another. The answer is you cannot directly get data from a console and for that you have to do marshelling. Since he said he do not want to go for that approach, sugggesting few other methods – Vimal CK Sep 24 '20 at 09:24
  • @VimalCK And processing of thousands of lines and formatting is not the issue here. There is no a big difference in the context of this task between putting data into a file and putting data into stdout. And your console application can produce output in json format as well. – Andrei Prigorshnev Sep 24 '20 at 09:26
  • @ErikA yes, you are right, using a COM object from Java can be a good option. – Andrei Prigorshnev Sep 24 '20 at 09:28
  • 1
    @VimalCK You can consume an output of (any) console application in Java [like this](https://stackoverflow.com/questions/20803664/get-output-of-cmd-command-from-java-code?noredirect=1&lq=1). And [here](https://stackoverflow.com/questions/186822/capturing-console-output-from-a-net-application-c) is an example how to do it in C#. – Andrei Prigorshnev Sep 24 '20 at 09:45

2 Answers2

1

Build a simple console application with C# and .NET Framework. This application should just go to the database and return data. Then call this console application from your Java application.

Additionally, consider creating a simple REST service with C# and .NET Framework and consuming it from your Java service. It can be an option if you already have some kind of microservice architecture. Otherwise, it can be overkill.

0

Third alternative is to build a DLL from C# code and use it in your java code.

Kavi
  • 140
  • 1
  • 9