Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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??

share|improve this question

3 Answers 3

up vote 4 down vote accepted

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.

share|improve this answer
    
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) –  Shrikant Sharat Apr 3 '11 at 10:03
    
@Shrikant Also, adding ~/bin to PATH may be good idea when doing it that way. –  Olli Apr 3 '11 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. –  Shrikant Sharat Apr 3 '11 at 10:43
    
@Shrikant: ah, true, you are right. It's automatically added in Ubuntu. –  Olli Apr 3 '11 at 10:44

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).

share|improve this answer
    
why downvote this answer? –  Shrikant Sharat Apr 3 '11 at 15:25
    
Tested your latter function, it works fine. –  Olli Apr 3 '11 at 18:31

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.