0

I need to create an executable as a result of a Makefile. Something as simple as if the user types abc, followed by the required input, in the command line the program executes. The code was written in Java. My Makefile runs just fine but I am not sure of how to create that executable. The name of the main method is Main.java just in case is necessary

The full execution would be something like:

abc xxxx

Where xxxx is the user's input. I am getting NullPointers when reading the input. I have tried using Scanner, BufferedReader, and System.console().system() and keep getting the same error.

Any advice on reading the input?

Thanks

3 Answers3

1

You can use the jar command to convert a Java class to an executable:

jar -cmf manifest.txt YourProgram.class

Provided you have a mainfest.txt manifest file with at least this row:

Main-Class: YourProgram

Anis R.
  • 6,656
  • 2
  • 15
  • 37
0

The right command once you compiled your program with javac is this:

jar cfn output.jar manifest.txt program.class

EDIT: The n option is deprecated, your can use m instead with no other change.

Where output is the name of your executable, manifest should contain at least one line:

Main-Class: program

and after that you can add as many classes as you need to.

Once you are done with that, the next step is to use some utility to convert the .jar to .exe . I recommend Launch4J since it is very easy to use and multi platform.

  • Could you explain what the `n` option does? I'm pretty sure you need to use `m` to use a manifest input file. – Scratte Apr 03 '20 at 05:29
  • I believe it does the same thing, specifies that you want to use the classes written in the manifest. – Alberto Caballero Apr 03 '20 at 06:20
  • You mean to say you didn't check before you posted this? I also don't think you can include the `.class` extension of the program entry classname in the manifest file. When you say "classes", do you mean to indicate that it's possible to specify **multiple** default program entry points? – Scratte Apr 03 '20 at 06:26
  • No, I did checked, that's what it is used for. When I say classes I mean that you can add your own dependencies. For instance add `Class-Path: MyUtils.jar` as a way to include your own custom classes. And you are right, the .class extension is not required. As for the `m` option the syntax doesn't really change: `jar cfm output.jar manifest.txt program.class`. You can learn more about it here [Working with Manifest Files: The Basics](https://docs.oracle.com/javase/tutorial/deployment/jar/manifestindex.html) – Alberto Caballero Apr 03 '20 at 06:42
0

Let's assume your command

abc xxxx

looks like one of these:

java Main xxxx
java -cp ... Main xxxx
java -jar ... xxxx

then you can access the second part xxxx like so:

public class Main {
    public static void main(String[] args) {
        System.out.println("main called with " + args);
    }
}

You cannot access the command line parameters on stdin or anywhere else, that is why your other attempts failed.

Queeg
  • 7,748
  • 1
  • 16
  • 42