-3

I'm actually not that great and am relatively new at Java. I wish to receive input from the user, and want to input this data into an external application.

This application processes the data and provides an output. I wish to retrieve this output using the Java code.

I have attempted in doing this but, I haven't got the slightest idea on how to start this script. Nothin' on the internet seems to answer this question. If you have any idea or any new functions that can be useful, please help me in doing so.

Since I'm starting from ground zero, any help is appreciated.

Thanks so much.

Abdulrahman Falyoun
  • 2,414
  • 1
  • 10
  • 31
Advik Raj
  • 5
  • 1

2 Answers2

0

To communicate with an external application you need to first define the communication way. For example:

  • Will this application read the output from a file?
    • If that statement it's true, then you need to learn serialization:
  • Will this application read the input from the standard output (like a command-line application)
    • If that statement it's true then you need to send with System.out.print().
  • Will this application get the data over HTTP.
    • Then you need to learn about REST and or RPC architectures.

Assuming that it will be a command-line application, then you could use something like this:

public class App
{
    public static void main(String... args)
    {
        // You need to implement your business logic here. Not just print whatever the user passes as arguments of the command-line.

       for(String arg : args)
       {
           System.out.print(arg);
       }
    }
}
Mysshell
  • 1
  • 1
0

There's a lot going on here but I'll suggest an example for each part of this question and assume this is just going to be written in Java, and suggesting an iterative design/development approach.

  1. receive input from the user::getting arguments from the command line can work, but I think most users want to use familiar user interfaces like excel to input large amounts of data. Have them export files to .csv or look into reading excel files directly with apache poi. The latter is not for beginners, but not terrible to figure out or find examples. The former should be easy to figure out if you look into reading files and splitting them line by line on the delimiter. Here's an example of that:

    try (BufferedReader reader = new BufferedReader(new FileReader(new File("user_input.csv"))) {
        String currentLine = reader.readLine();
        while (currentLine != null) {
            String splitLine[] = currentLine.split(","); //choose delimiter here
            //process cells as needed
            //write output somewhere so other program can read it later
            currentLine = reader.readLine();
        }
    }
    catch (IOException ex) {
        System.out.println(ex.getMessage()); //maybe write to an error log
        System.exit(1);
    }
    
  2. "input" data to other app::you can use pipes if you're at the command line. but I'd recommend you write to a file and have the other app read it. here's an expansion of the previous code snippet showing how to write to a file as that might be more practical and easier to log/archive/debug.

    try (BufferedReader reader = new BufferedReader(new FileReader(new File("user_input.csv")));
        BufferedWriter writer = new BufferedWriter(new FileWriter(new File("process_me.csv")))) {
        String currentLine = reader.readLine();
        while (currentLine != null) {
            String splitLine[] = currentLine.split(","); //choose delimiter here
            //process cells as needed
            writer.write(processed_stuff);
            currentLine = reader.readLine();
        }
    }
    catch (IOException ex) {
        System.err.println(ex.getMessage());
        System.exit(1);
    }
    
  3. Then retrieving output::can just be reading another file with another Java program. This way you're communicating between programs using the file system. You must agree upon file formats and directories though. And you'll be limited to having both programs on the same server.

To make this at scale, you could use web services assuming the other program you're making requests to is a web service or has one wrapped around it. You can send your file and receive some response using URLConnection. This is where things will get much more complex, but now everything in your new program is just one Java program and the other code can live on another server.

Building the app first with those "intermediate" files between the user input code, the external code, and the final code will help you focus on perfecting the business logic, then you can worry about just communication over the network.

bitbangs
  • 466
  • 3
  • 6