1

I have created one java program on my Linux system which indents and formats the given file. I want to make that program work like a command in Linux which will take file names and other options as arguments and then will produce the output. I can do this with a C program by just copying the compiled executable in /bin folder but I don't know how to do it with java.

4
  • You created a program to indent and format file. That's good. You might be please to know that there exists a program called indent that already does this for you. Commented Jul 20, 2013 at 10:30
  • 3
    With Java you write a small shell script that invokes Java on your Jar, and passes through some of the parameters passed to the shell script. Commented Jul 20, 2013 at 10:31
  • gnu.org/software/indent Commented Jul 20, 2013 at 10:31
  • 1
    well thanks devnull for the information about indent software but I did this as my college mini-project! and dasblinkenlight can you give me the example of a shell script? I know very little about shell scripting! Commented Jul 20, 2013 at 10:34

4 Answers 4

1

Sample script that can might further help-

#!/bin/bash

#Set whatever number of arguments you expect for the Java jar you have
ARGS_EXPECTED=3

if [ $# -ne $ARGS_EXPECTED ]
then
  echo "[$HOSTNAME]: Usage: `basename $0` filename arg1 arg2"
  exit 1
fi

java -cp yourfile.jar com.yourpkg.Driver $1 $2 $3

Save the above content to a file, say test.sh and use the command to give an executable permission chmod +x test.sh

run like ./test.sh filename arg1 arg2 from current directory where test.sh is

Sign up to request clarification or add additional context in comments.

Comments

0

I thing this can be useful for your case: http://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/tree/Documentation/binfmt_misc.txt?id=HEAD

Comments

0

You can simply run a Java class file or jar file with "java" command from command line. Example:

java -jar yourprogram.jar argument1 argument2

If you save this line in a text file saved for example as "script.sh" and then give it the permission for execution you can run it double clicking or from terminal typing ./script.sh in the same folder containing the file script.sh.

You can also produce scripts that use arguments with $1 $2 etc. avoiding the need of editing file. http://www.linuxquestions.org/questions/linux-newbie-8/how-to-pass-command-line-parameter-to-shell-script-254396/ You can use named parameters, too.

You can also produce a C program for a new command like you suggested that run the "java" command. In this case you can introduce arguments directly from terminal and pass them to java command in the C source.

2 Comments

It seems that using .class file as a command is not as easy as a C program executable.. but anyway thanks Ufo.rob for your help!
You need to pack your application correctly within a .jar for this to work; it seems an additional description to do his would be necessary for completenesss.
0

As others have pointed out it is probably best to use a small shell script to run the Java application. There are several open source products that will help you wrap your Java code to produce a runnable (set of) .jar(s).

If you have correctly separated your business logic from your interface (as you should) then it is probably best if your Java application parses the parameters given on the command line interface. To do this create a separate class for parsing such parameters and calling the classes making up the business logic. Of course this will lead quickly - if not immediately - in writing a parser for Linux like CLI parameters. When this happens you may wish to consider the Apache Commons CLI project.

If you don't want to use any wrapping application/runtime, my method is generally pointing to all the class file containers in the classpath and directly pointing to the class containing the static main method:

java -cp "path_to_jar;path_to_class_folder;etc" "nl.owlstead.stackoverflow.LinuxMain"

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.