I am writing a bash script that automates installation of TLauncher (Minecraft launcher) and basic dependency management on USBs running Lubuntu 18.04.
Part of the bash script creates a desktop entry under /usr/share/applications for easy launch from the Lubuntu menu.
Here is the entry I am using:
[Desktop Entry]
Encoding=UTF-8
Exec=pkexec env DISPLAY=$DISPLAY XAUTHORITY=$XAUTHORITY /usr/bin/java -jar -Dswing.systemlaf=javax.swing.plaf.nimbus.NimbusLookAndFeel ~/.minecraft/tlauncher.jar
Icon=~/.minecraft/icons/default.png
Type=Application
Terminal=false
Name=Minecraft
GenericName=minecraft
StartupNotify=false
Categories=Game
The command pkexec env DISPLAY=$DISPLAY XAUTHORITY=$XAUTHORITY /usr/bin/java -jar -Dswing.systemlaf=javax.swing.plaf.nimbus.NimbusLookAndFeel ~/.minecraft/tlauncher.jar works and does launch TLauncher if run in the terminal. However, I feel I am missing something when I put the command in the Desktop Entry.
When I try to open the entry from the Lubuntu menu, the cursor shows that something is loading, but then nothing happens no matter how long I wait. I am also not sure how to debug the Desktop entry. I tried toxdg-open the entry, but I got no output in the terminal and nothing happened either -i.e, TLauncher did not launch.
I tried putting the command in a separate bash file that looks like:
#!/bin/bash
pkexec env DISPLAY=:0 XAUTHORITY=/home/me/.Xauthority /usr/bin/java -jar -Dswing.systemlaf=javax.swing.plaf.nimbus.NimbusLookAndFeel ~/.minecraft/tlauncher.jar
Running that in the terminal bash testscript.sh works and does launch TLauncher.
But when I put it in the desktop entry replacing the exec from the entry above like so:
Exec=/bin/bash /path/to/testscript.sh
It does not work.
The shell scripts have the proper permissions.
I feel like either I'm writing the Desktop Entry's exec value wrong or that pkexec isn't supposed to work from a Desktop Entry the way I am trying to make it work.
I would like to be able to launch TLauncher from a desktop entry via the menu and have a gui popup asking for the password in order to have the command run as root.
Edit:
Using full paths instead of ~ also yields the same result -i.e, it does not work and the above issues remain.
Setting Terminal=true does not solve this for me.
Current solution: Since the command works when run in the terminal, simply have the Desktop Entry run the command with a terminal:
Exec=lxterminal -e "pkexec env DISPLAY=$DISPLAY XAUTHORITY=$XAUTHORITY /usr/bin/java -jar -Dswing.systemlaf=javax.swing.plaf.nimbus.NimbusLookAndFeel /home/me/.minecraft/tlauncher.jar"
Notes:
I think I could build off of this to make sure that when the launcher is closed, it is actually closed as I noticed through ps aux that processes aren't actually being killed the way I expect causing strange behavior the next time I open the launcher.
Related
I want to automatically run a Java application under Windows at startup time, but not depending on a specical Windows or a special JRE-version.
Generally, a good way to run programs at Windows login time would be to add a registry entry under
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run
But it seems as if the system PATH is not evaluated when Windows tries to run the programs listed in the registry's RUN section.
So these both entries do work:
"C:\Program Files\Java\jdk1.8.0_111\bin\javaw" -jar c:\path\to\java.jar
"C:\ProgramData\Oracle\java\javapath\javaw" -jar c:\path\to\java.jar
But this one doesn't come up:
javaw -jar c:\path\to\java.jar
Now, the latter works when manually entered in a console window because there the required PATH exists and is evaluated:
PATH=....;C:\ProgramData\Oracle\Java\javapath;....
Now, I want to have the registry entry added from an automated installer programm and I would like to have it formed "windows-system-independent" (from Vista up to Win 10, and furthermore not depending from the version of the installed JRE). So I do not know a general path to the JRE and therefore I cannot add a specific RUN-command to the registry.
Does anyone know how to add JRE-calls system-independent correctly to the registry's RUN-section?
I've already tried with a command-file, e.g. "startup.cmd" with content java -jar file.jar there, have added a call of this file to the registry, and yes that worked, java was found, the application was started, but when using this method I have an annoying black command terminal window staying open all the time while the java app is running.
Thanx,
Tombo
Change your startup.cmd, from
java -jar file.jar
to
start java -jar file.jar
This will launch the java.exe in a new process an let your batch executor terminate.
I have azure-cli installed in my mac and I can successfully execute all azure commands from cli. I want to do the same from a java application. I have seen examples of using REST endpoints and azure-sdk-for-java. But what I want is to execute the azure-cli commands directly from java.
I tried running 'azure login' from a java program, but I got the following error.
Cannot run program "azure": error=2, No such file or directory
The problem is in a lack of your PATH variable. If you want to enjoy the same "find the command" capabilities that you have on the command line, you need to set it somehow within your Java program: import the PATH variable from the environment, set it within the code you're writing, or whatever mixture covers your needs.
Giving the absolute (full) path is the surest way, but you may not want to read such long command names in your source codes.
#harshithabt Per my experience, the issue was caused by the command azure could not be searched in the directories listed in PATH. There are two ways to solve the issue for running commands in Java, please see below.
Setting up the environment variable PATH for the current shell session or the configuration files ~/.profile(or ~/.bashrc).
If you command export PATH=<your-node-path>/bin:$PATH in a shell session, you only run the Java program with azure command in the current shell session, even run via the Java IDE which must be opened in the current shell session (it means you should open Eclipse via command <your-ide-path>/eclipse).
If you configure the files ~/.profile or ~/bashrc to add the node runtime as below, please make sure the configuration files have been reloaded via command source ~/.profile in a current session or restart sessions via logout & login or reboot.
A simple way is that adding the command links into the dirs listed in PATH, such as /bin. You just need to run the commands below.
sudo ln -s /bin/node /bin/node
sudo ln -s /bin/azure /bin/azure
Then you can run azure-cli command from Java successfully.
Environment: mac
Precondition: I already configured 'adb' full path to my bash_profile. and when I tried type 'adb' in my terminal, it is working.
But, I tried to exec 'adb' command from java, 'adb' is not working, instead I need to pass the full adb path to make it work.
I guess this is probably something to do with the bash_profile setting, anyone know the exact reason for this issue?
Runtime.getRuntime().exec() runs /bin/sh -c <command>. If this is or points to a bash shell on your system: A non-interactive bash does not read .bash_profile unless explicitly (--login) told to do so.
From the documentation:
When Bash is invoked as an interactive login shell, or as a non-interactive shell with the --login option, it first reads and executes commands from the file /etc/profile, if that file exists. After reading that file, it looks for ~/.bash_profile, ~/.bash_login, and ~/.profile, in that order, and reads and executes commands from the first one that exists and is readable. The --noprofile option may be used when the shell is started to inhibit this behavior.
It's a little convoluted, but non-interactive, non-login bash instances don't read the profile files.
Your path settings does not get picked up by the subshell (which is actually /bin/sh which might not even be bash at all).
If you want, you can add the path to adb system wide by adding an appropriate entry to to /etc/paths.d.
I have a java web application project. I am using the exec-maven-plugin to execute a shell script which creates a small txt file in a directory when the project is built using mvn clean isntall. I have confirmed that the script is being executed when running mvn clean install by writing test text. However, the script is not creating the txt file.
When the script is run normally through the terminal, ie ./script.sh , the txt file is created correctly.
toTxt="hello"
printf $toTxt > testText.txt
echo 'This shows up, but testText is not created.'
Does anyone know the reason why this is happpening?
Try either echoing pwd command to see where it should create the file or add <workingDirectory>${project.build.outputDirectory}</workingDirectory>
to your configuration block in pom.xml
NOTE: You can specify something other than ${project.build.outputDirectory} to specifically point to the right place. And make sure you have write permissions to it.
Keep in mind that some commands are not really external programs, but shell built-ins. This means that they don't launch programs, and maven-exec-plugin will not be able to run them. However, you can run bash to get the result, just not pwd.
bash -c "echo $(pwd)"
should do the trick. Maven is launching bash which is then running the script echo $(pwd) which calls the bash builtin function pwd and passes the result back (due to the echo).
Yes, it's a lot of work to get around the lack of a pwd program, but that's because there really isn't a pwd program, it's part of the internal offerings of bash.
http://www.tldp.org/LDP/abs/html/internal.html lists the bash internals (builtin commands).
So far i've created a service using automator to compile a Java file. This means that i can compile a java file through its contextual menu.
Unfortunately I cannot do the same with running the Java file. Through automator I can get it to run the java file perfectly, showing the results in the automator window, however it will not open the Terminal window to view the java file. I did this using this code:
java -classpath `dirname "$1"` `basename "$1" | sed "s/.class//g"`
Applescript on the other hand can run Terminal commands into a Terminal window for all to see using code such as:
on run {input, parameters}
tell application "Terminal"
activate
do script with command "java -classpath" & input & input
end tell
end run
The correct way to run a java file is:
java -classpath /path/to/ file
Can anybody please help?
Thanks in advance.
Utilities > Run Shell Script with Passing input: as arguments seems to work in this context.
Addendum: If you want to open Terminal, something like this may work.
open -a Terminal ~/your/script.sh