5

I have a Main.java file and I want to run the program passing it test.txt

I know in command line I can write javac Main.java

After compiling I can write java Main test.txt and this will accomplish running the file and passing test.txt

If I wanted instead to be able to just write main test.txt and have that trigger my Main.class file to run is that possible and if so how?

Nosrettap
  • 10,940
  • 23
  • 85
  • 140
brucebomber
  • 93
  • 1
  • 8
  • You didn't do anything wrong. :) Cool question. It probably has more to do with OS-specific things than Java, though, as you'll probably have to do some shell modifications or something along those lines. – asteri Sep 21 '12 at 21:15

3 Answers3

1

add an alias e.g. under a mac edit your .bash_profile with the following line

alias main='java main'

don't forget to open a new console to see your alias working

luko
  • 121
  • 6
1

(Edit: Based on your comment, let me expand to add a couple more situations)

If your goal is to have someone else run your program who does not have Java installed, and you do not wish to have them install a Java runtime environment before running your app, what you need is a program that converts the .class or .jar files into a native executable for the platform you are using. How to do this has been covered in other questions, eg: Compiling a java program into an executable . Essentially, you use a program like JCG (GNU Compiler for Java) or Excelsior JET (a commercial product) to expand the byte code into full native code with a mini-JRE built in.

If your goal is to save typing, there are a number of strategies. Others have suggested alias commands, which work well on linux.

A slightly more portable option that you could ship with your program would be a shell script. Granted, shell scripts only run on linux or other OS's with shell script interpreters installed.

Here is an example shell script. You paste this into a text editor and save it as main with no extensio. The $1 passes the parameter argument fyi.

#!/bin/sh
java Main $1

presuming you name your shell script just "main" with no extension, you could call main test.txt to execute your program now.

If you are on Windows, you might want to create a windows shortcut, and point the shortcut to "java Main test.text", using the full paths if necessary (if the paths are not already set). Of course, this does not make the parameter easy to change every time you run it, you would have to edit the shortcut.

Community
  • 1
  • 1
Jessica Brown
  • 8,222
  • 7
  • 46
  • 82
0

Depends on your operating system. On Linux with the bash shell, for instance, you can set up an alias to expand your main into java -cp myjar.jar main.

Linux can also be configured to 'understand' Java class flies as a binary format directly see here (linux kernel documentation).

If you're on windows, you'll have to wait for answer from someone with more knowledge about that than I.

Good luck!

Faelkle
  • 476
  • 3
  • 6
  • Also see [this] (http://stackoverflow.com/questions/1667830/running-a-jar-file-without-directly-calling-java), which points to the same solution. – Faelkle Sep 21 '12 at 21:17
  • 1
    You could create a short cut to `java.exe` passing in the command line parameters under windows. You'd just have to make sure that the launch context is pointing to the correct directory – MadProgrammer Sep 21 '12 at 21:18
  • Yep. For linux/cygwin: OP should also check out Groovy shell scripts; they can run java (+Groovy) code in a similar manner to normal shells scripts. Ref: [Creating Unix Scripts With Groovy](http://groovy.codehaus.org/Running). – Faelkle Sep 21 '12 at 21:29