LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   How to add Java applications to panel (https://www.linuxquestions.org/questions/linux-software-2/how-to-add-java-applications-to-panel-597826/)

netjack 11-07-2007 12:51 PM

How to add Java applications to panel
 
Hi,

I have some Java applications written by myself and would like to place executable buttons in the graphical Desktop panel. For instance, I have my jar file, then I have another file "MyExecutable" that has the command java -jar myfile.jar, which I have made "executable", i.e. if I double click on it, ubuntu asks me if I want to run the program or show its contents.

I'd like to have a button in the gnome panel I could start the program with; when I configure the custom launcher dialog, I do not know what to insert in the "command" field (/home/Desktop//.MyExectuable does not work).

Thanks for your help.

b0uncer 11-07-2007 12:52 PM

Code:

/home/Desktop/MyExecutable
should work, if there's nothing curious about it.

EDIT: you launch it like "./MyExecutable" from the terminal if your working directory is the directory where the executable is, because the dot file "." means the current directory. Therefore "./" means "inside the current directory", and "./MyExecutable" means "run MyExecutable from within this current directory". You can also run it with full path, like "/home/username/Desktop/MyExecutable", which is what you use with the launcher, because the launcher's "current working directory" isn't necessarily what you want, if it even is aware about such a thing.

netjack 11-07-2007 01:30 PM

Quote:

Originally Posted by b0uncer (Post 2951224)
Code:

/home/Desktop/MyExecutable
should work, if there's nothing curious about it.

Code:

/home/myname/Desktop/MyExecutable
does not work. Thanks for the explanations about the ./ ;)!

This is how I created the "MyExecutable": with gedit in a new file, added "java -jar MyJavaApp.jar". Wrote to disk with no extension. With gnome, under "permissions", I changed it to executable. Gnome however still recognizes this of type "plain text document".

reddazz 11-07-2007 04:28 PM

Try entering the commands as
Code:

java -jar /path/to/myfile.jar

osor 11-07-2007 04:31 PM

Quote:

Originally Posted by netjack (Post 2951255)
Gnome however still recognizes this of type "plain text document".

Two things:
  1. Try making your file a valid shell script (i.e., complete with she-bang followed by interpreter). This doesn’t require too much work:
    Code:

    #!/bin/sh

    java -jar /home/myname/Desktop/MyJavaApp.jar

  2. If you want to be cool, you can register a jar-file loader with the kernel (using binfmt_misc). That is, you can teach the kernel to recognize the extension “.jar” (or, in the class of naked .class files, the magic number 0xCAFEBABE), and associate it with a loader (which is probably a shell script you write). Then, all that’s required to execute a java program is:
    Code:

    chmod +x MyJavaApp.jar
    ./MyJavaApp.jar

    Or
    Code:

    chmod +x MySmallJavaApp.class
    ./MySmallJavaApp.class

If you need help with the second method, just post.

netjack 11-08-2007 12:47 PM

Quote:

Originally Posted by reddazz (Post 2951424)
Try entering the commands as
Code:

java -jar /path/to/myfile.jar

Thanks, this worked !

netjack 11-08-2007 12:49 PM

Quote:

Originally Posted by osor (Post 2951429)
Two things:
[*]If you want to be cool, you can register a jar-file loader with the kernel (using binfmt_misc).

That would be nice indeed. How do I activate (and, possibly) deactivate that in the kernel?

Thx! :)

osor 11-08-2007 03:57 PM

Quote:

Originally Posted by netjack (Post 2952385)
How do I activate (and, possibly) deactivate that in the kernel?

Before we do this, let’s create a jar-file loader (which we will later use). The prospect of this loader is that you execute your program:
Code:

jarloader /path/to/program.jar
We will place it in /usr/local/bin so it doesn’t interfere with system files (all further excerpts assume you have root access):
Code:

cat > /usr/local/bin/jarloader << EOF
#!/bin/sh

/usr/bin/java -jar $@
EOF

(you might change the /usr/bin/java so it matches your actual java interpreter)

Let’s make it executable:
Code:

chmod +x /usr/local/bin/jarloader
Now let’s get on with using binfmt_misc.The concept is pretty simple (a more detailed explanation may be found here). First, you load the binfmt_misc module:
Code:

modprobe binfmt_misc
Then, you mount the virtual binfmt_misc filesystem. This will allow you to see and register miscellaneous “binary formats” with the kernel. The canonical place to mount it is within the /proc directory itself:
Code:

mount -t binfmt_misc none /proc/sys/fs/binfmt_misc
Now, we register our file by writing a string containing a sequence of colon-delimited fields (which are explained in detail in the documentation) to the file register:
Code:

echo ":JAR:E::jar::/usr/local/bin/jarloader:" > /proc/sys/fs/binfmt_misc/register
You now see new file in the virtual filesystem. Its contents may be viewed by simply reading them:
Code:

cat /proc/sys/fs/binfmt_misc/JAR
You may temporarily disable this format by writing a 0 to it:
Code:

echo "0" > /proc/sys/fs/binfmt_misc/JAR
It may be re-enabled by writing a 1 to it:
Code:

echo "1" > /proc/sys/fs/binfmt_misc/JAR
It may be removed completely by writing -1 to it:
Code:

echo "-1" > /proc/sys/fs/binfmt_misc/JAR
A different solution may be found for naked class files (usually shorter programs). It is different for two reasons:
  1. The java interpreter does not expect the full path to the class file as an argument. Instead, it assumes you have a suitable definition of CLASSPATH and that you specify the name of the class itself (without the extension). So your wrapper program may look more like this (which is a tad more complicated).
  2. Instead of matching the extension (which is somewhat crude), we can use the fact that all java classes begin with the first four bytes 0xCAFEBABE. So instead of matching an extension, we match a magic number. So your registration might end up looking like:
    Code:

    echo ':Class:M::\xca\xfe\xba\xbe::/usr/local/bin/javawrapper:' > register
    instead.
You can also add the relevant commands to your startup scripts to get automatic registering of formats.


All times are GMT -5. The time now is 06:51 PM.