2

I am tired of

 javac filename.java

and then

 java filename

isn't there a way to make my own command?

mycommand filename.java

And the filename.java is both compiled and run??

3 Answers 3

5

You can create really small bash script:

#!/bin/bash
javac "$1"
java `basename "$1" .java`

Put this to /usr/local/bin/runjava.sh, and execute

sudo chmod 755 /usr/local/bin/runjava.sh

Then you can just type

runjava.sh filename.java

to compile and run it.

Alternatively, you can put runjava.sh (or your own name to it) to ~/bin folder. If that folder do not exist, you can create it with command mkdir ~/bin. After opening new terminal, it's in your PATH automatically (assuming Ubuntu and bash, without .bashrc customizations).

First method adds it to all users in your system, second one only to you.

4
  • I'd suggest putting the sh file in ~/bin directory, so it is easy to access and will be available for this user only. (Not that your location is any wrong, just saying)
    – sharat87
    Apr 3, 2011 at 10:03
  • @Shrikant Also, adding ~/bin to PATH may be good idea when doing it that way.
    – Olli
    Apr 3, 2011 at 10:09
  • 1
    I think it is added to PATH by default, if the directory exists... am not on ubuntu right now, so, can't check.
    – sharat87
    Apr 3, 2011 at 10:43
  • @Shrikant: ah, true, you are right. It's automatically added in Ubuntu.
    – Olli
    Apr 3, 2011 at 10:44
2

So, assuming bash (You are running ubuntu right? ;) ), you can create a function for this...

j () {
  javac "$1" && java "${1%.*}"
}

and then if you have a file called PureJavaAwesomeness.java, you could do

j PureJavaAwesomeness.java

and that should compile and run the java file.

Edit: And, you might want to put that function definition at the end of your ~/.bashrc file, so it is loaded every time you open your terminal.

Edit2: For some more awesomeness, you can use this function

j () {
  cname=${1//\//.}
  cname=${cname%.*}
  javac "$1" && java "$cname"
}

This will work even when you do j com/ssk/apps/PackagedClass.java, assuming the package of the PackagedClass.java is com.ssk.apps. (Not tested, but should work).

2
  • why downvote this answer?
    – sharat87
    Apr 3, 2011 at 15:25
  • Tested your latter function, it works fine.
    – Olli
    Apr 3, 2011 at 18:31
1

THIS IS AN OVERKILL FOR WHAT YOU NEED BUT SOMEONE ELSE MIGHT STUMBLE UPON THE QUESTION SEARCHING FOR "HOW TO MAKE OWN COMMANDS ON UBUNTU".

You can use quickly ubuntu-cli-application template to create commands and easily package them as debs or share using ppas.

You'll need some knowledge of python though.

First install Quickly from the Software Center.

Then in the terminal type,

quickly create ubuntu-cli my-program-name

This will create a project directory for you complete with command line handling structure. All you have to do is to edit the files to add your own commands.

Once done, you can share your new commands with friends using

quickly package

This will create a DEB package ready to be installed on any Ubuntu machine.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .