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 want to make a script that will execute .jar file on selected file. Later I will add that script to right-click menu via the tool Nautilus Actions or just place it into nautilus-scripts folder. I have a problem creating script.

When I am in a usual console screen and want to execute this jar file on any other file, I use this syntax

myfile.jar ./someotherfile.xml

and the jar file will write the output to the console screen.

So I created a file script.sh, added lines in it

#!/bin/bash
/home/username/myfile.jar $1

But it does not output anything. I know I am doing something wrong. Please help.

To sum, I need a script that will use selected file as a parameter, open the gnome-terminal, inside that terminal it will start JAR file and pass it the selected file.

I am confident that this is a very simple procedure, but I am total newbie with shell scripting.

share|improve this question
1  
Don't know if that's a tipo, but you probably want /home/askmoo/myfile.jar. –  mikewhatever Jun 23 '11 at 9:48
    
you're right. I've edited the question. Thanks –  ubuntico Jun 23 '11 at 11:20

3 Answers 3

up vote 2 down vote accepted

You might also go for

!/bin/sh
gnome-terminal -x java -jar /home/askmoo/myfile.jar "$1"

in order to first open gnome terminal and then execute your java application in it. This way you would be able to get output printed out to the terminal.

share|improve this answer
    
It opens a new terminal for microsecond and closes. How can I make this new terminal not closing immediately? –  ubuntico Jun 23 '11 at 10:54
1  
By opening up your terminal manually - and editing your profile settings - there's a dropdown menu for it in the 2. tab –  tesseract Jun 23 '11 at 10:57
    
Great, now it works. One more question: Does symbol "$1" represent the file currently selected? This script is in nautilus-script directory –  ubuntico Jun 23 '11 at 11:07
2  
"$1" refers to the first argument of a command, e.g. command first-arg. If you drop a file on the script, the first argument becomes the file path of that file. –  Lekensteyn Jun 23 '11 at 12:01
    
This script works out from nautilus-scripts directory. But if I add it to the right-click menu via Nautilus Actions tool, it does not work. And I simply just call this script from the Actions configurations. I put %f for parameter. Should I open a special question for this? –  ubuntico Jun 23 '11 at 12:09

There is always a "right click properties" pop-up to executables (and icons) on the desktop and folders in Gnome, KDE and others. I'm not sure how you would go about this with the nautilus actions extension but you can always force your terminal to hang after a command exits like this:

$ cat > /usr/local/bin/har.sh
#!/bin/sh
java -jar /home/jaroslav/tmp/src/hat/bin/hat.jar
while true;do  
read  -p "finished reading? " quit; 
[[ "$quit" == "yes" ]] && exit
done 

$ chmod 755 /usr/local/bin/hat.sh
$ hat.sh

Usage:  hat [-stack=<bool>] [-refs=<bool>] [-port=<port>] [-baseline=<file> -debug=<int>] [-version] <file>

        -stack false:     Turn off tracking object allocatoin call stack.
        -refs false:      Turn off tracking of references to objects
        -port <port>:     Set the port for the HTTP server.  Defaults to 7000
        -exclude <file>:  Specify a file that lists data members that should
                          be excluded from the reachableFrom query.
        -baseline <file>: Specify a baseline object dump.  Objects in
                          both heap dumps with the same ID and same class will
                          be marked as not being "new".
        -debug <int>:     Set debug level.
                            0:  No debug output
                            1:  Debug hprof file parsing
        -version          Report version number
        -donationware     Give information on the status of HAT
        <file>            The file to read

For a JDK 1.2 (or better) dump file, you may specify which dump in the file
by appending "#<number>" to the file name, i.e. "foo.hprof#3".

All boolean options default to "true"

finished reading? no
finished reading? ^C
$

give the terminal CTRL+C to exit (or type yes)

there is also the properdies dialog: .. ok I just looked through nautilus properties... no luck (I f* hate gnome, they keep making it dumber and dumber)

Its still available in KDE: (but id as soon just use the infinite-while loop), the number of boxes to navigate to get to those settings is just overwhelming.
omg-so-many-boxen

share|improve this answer

./myfile.jar and myfile.jar are two different commands. The first executes a file in the current directory (providing the execute bit is set), the latter searches $PATH for a program named myfile.jar.

If $PATH is /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games, then it searches for (in order):

/usr/local/sbin/myfile.jar
/usr/local/bin/myfile.jar
/usr/sbin/myfile.jar
/usr/bin/myfile.jar
/sbin/myfile.jar
/bin/myfile.jar
/usr/games/myfile.jar

I doubt if your jar file is really an executable, if it's a Java ARchive, your shellscript should be:

#!/bin/sh
java -jar /home/askmoo/myfile.jar "$1"
share|improve this answer
    
It is executable and it does do its job when I enter its name in the console. –  ubuntico Jun 23 '11 at 10:46
    
Could you add the output of file "$(which myfile.jar)" to your question? –  Lekensteyn Jun 23 '11 at 10:47
    
Output is "Zip archive data, at least v2.0 to extract" –  ubuntico Jun 23 '11 at 10:56
    
@askmoo: that's indeed a Java ARchive file. Have you tried java -jar /path/to/your.jar? –  Lekensteyn Jun 23 '11 at 11:00
    
sure and it works both ways :). please do not ask me how. –  ubuntico Jun 23 '11 at 11:03

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.