JavaFX - Virtual Keyboard Doesn't Show When the App Jar is Generated - java

I have created a JavaFX application in IntelliJ14.14 that will use the JavaFX Virtual Keyboard. I have add the following properties in the mainApp class controller:
public static void main(String[] args) {
System.setProperty("com.sun.javafx.isEmbedded", "true");
System.setProperty("com.sun.javafx.touch", "true");
System.setProperty("com.sun.javafx.virtualKeyboard", "javafx");
launch(args);
}
When I run the app from IntelliJ everything works fine. The virtual keyboard works perfectly.
But when I generate the Jar file of the application from Build -> Build Artifacts... -> Build and execute it, the keyboard never shows because the VM options are not being set.
It's something I'm missing...?
Thanks in advance...
EDIT
I have found a way to make this work, running the file from the cmd with this command:
java -Dcom.sun.javafx.isEmbedded=true -Dcom.sun.javafx.virtualKeyboard="javafx" -Dcom.sun.javafx.touch=true -jar myApp.jar
However I want to make this just executing the Jar file...
EDIT
There is another nearby way to accomplish what I want...
Create a file .bat in the same folder of the jar and put in it:
start javaw -Dcom.sun.javafx.isEmbedded=true -Dcom.sun.javafx.virtualKeyboard="javafx" -Dcom.sun.javafx.touch=true -jar myApp.jar
So when tha .bat file is executed and the jar file is started, the system properties are loaded correctly...

public class MyApp {
public static void main(String[] args) {
if (args.length == 0) {
try {
// re-launch the app itselft with VM option passed
Runtime.getRuntime().exec(new String[] {"java", "-Dcom.sun.javafx.isEmbedded=true", "-Dcom.sun.javafx.virtualKeyboard=\"javafx\"", "-Dcom.sun.javafx.touch=true", "-jar", "myApp.jar"});
} catch (IOException ioe) {
ioe.printStackTrace();
}
System.exit(0);
}
// Run the main program with the VM option set
//...
//...
}
}

Related

Java application not reading environment variables from .env file

I am creating a Slack bot using Bolt framework in Java + Maven following this tutorial on their official website. My bot requires some keys that should be present as environment variables.
The application works just fine when I export those variables in the terminal prior to executing the application like this.
export SLACK_BOT_TOKEN=xoxb-...your-own-valid-one
export SLACK_SIGNING_SECRET=123abc...your-own-valid-one
mvn compile exec:java -Dexec.mainClass="hello.MyApp"
But when I don't, and instead save them in a .env file, the App constructor apparently doesn't get those keys even though I have loaded them in the main method from .env file.
Here is my main method:
package hello;
import com.slack.api.bolt.App;
import com.slack.api.bolt.jetty.SlackAppServer;
import io.github.cdimascio.dotenv.Dotenv;
public class MyApp {
public static void main(String[] args) throws Exception
{
Dotenv dotenv = Dotenv.configure().directory("./").load();
System.out.println(dotenv.get("SLACK_BOT_TOKEN"));
System.out.println(dotenv.get("SLACK_SIGNING_SECRET"));
// CREATING THE SLACK APP
App slackApp = new App();
slackApp.command("/mytesthello", (req, ctx)-> {
return ctx.ack(":wave: Hello!");
});
SlackAppServer slackAppServer = new SlackAppServer(slackApp);
slackAppServer.start();
}
}
The console is able to show the correct output using System.out.println indicating that the variables are accessible, but most probably the App() constructor is not able to get them, since whenever I try to make POST request from my slack app, the ngrok interface as well as my terminal show unauthorized access. (again, this is the case when I don't export the environment variables)
What should I do to make sure that the environment variables are available from .env file? Otherwise it would be very cumbersome to keep exporting them whenever I run the application.

How to execute 'cd' command and then a linux command in java

I have directory /tmp
then I need to execute cd and go to that folder.
Then I need to execute ./executeScript
Preparation
To start solving the problem, I created a directory /home/vulpini99/tmp. In this directory, I created the bash-script test.sh, which will open firefox for us:
firefox
Then I created a java-file named LinuxCommand.java in the directory /home/vulpini99.
Main part
cd is just an internal shell command and not an executable program, so I suggest to just use the full path of the bash-script. So the command we want to execute is
bash /home/vulpini99/tmp/test.sh.
In Java, you can use Runtime for this purpose:
import java.io.IOException;
public class LinuxCommand {
public static void main(String[] args) {
Runtime run = Runtime.getRuntime();
try {
run.exec("bash /home/vulpini99/tmp/test.sh");
}
catch(IOException e) {
e.printStackTrace();
}
}
}

Autostart .jar executable is not working

I want to start my Java Application (a Discord bot) when I boot my windows 10 PC.
To do that. Also, I have a bat file in my Autostart folder with the following:
start javaw -Xmx200m -jar D:\Local\bot.jar
EXIT
The main of the Java Code has the following:
public static void main(String[] args) {
try {
BotListener NL = new BotListener();
jda = new JDABuilder(AccountType.BOT).addListener(NL).setToken(BOT_TOKEN).buildBlocking();
} catch (LoginException | IllegalArgumentException | InterruptedException | RateLimitedException e) {
e.printStackTrace();
}
I've tried shutting it down and creating a new jda after a delay in a thread, both with the delay in the main thread and in another thread class. I also tried shutting it down and then running the bat file again.
When I run the bat file it works. Only when it's run automatically on startup it fails. I am assuming it's a problem with Java or using a .bat and a .jar or something because restarting the jda worked when I run it myself.

Running .JAR file from java code (netbeans)

Im trying to run a jar file from java code, But unfortunately does not success.
A few details about the jar file:
The jar file located in a different folder (For example - "Folder").
The jar file using a files and folders are in the root folder (the same "Folder" i mentioned above).
What im trying to do so far:
JAR file project.
In netbeans i checked that the main class are defiend (Project properties -> Run -> Main Class).
Other JAVA program
Trying to run with the command:
Runtime.getRuntime().exec("javaw -jar "C:\\Software\\program.jar");
&&
Runtime.getRuntime().exec("javaw -jar "C:\\Software\\program.jar" "C:\\Software");
The jar file opened well, But he doesnt know and recognize his inner folders and files (the same "Folder" i mention above).
In short, it does not recognize its root folder.
Trying to run with ProcessBuilder
ProcessBuilder pb = new ProcessBuilder("cmd.exe", "/C", "start", "javaw", "-jar", "C:\\Software\\program.jar");
pb.directory(new File("C:\\Software"));
try {
pb.start();
} catch (IOException ex) {
}
In Some PC's its works fine, But in other pc's its not work and i got an error message: "Could not find the main class"
** Offcourse if i run the jar with double click its works.
So how can i run a jar file from other java program ?
Use this variant of .exec where you specify working folder as the third argument. (In your examples, you always only use one argument.)
exec("javaw -jar "C:\\Software\\program.jar", null, "C:\\Software");
You can try to call it something like below. There are 2 types of calling it.
public class JarExecutor {
public static void main(String[] args) throws IOException, InterruptedException {
//This is first way of calling.
Process proc=Runtime.getRuntime().exec(new String[]{"java","-jar" ,"C:\\Users\\Leno\\Desktop\\JarsPractise\\JarsPrac.jar"});
//This is second way of calling.
Process proc=Runtime.getRuntime().exec(new String[]{"java","-cp","C:\\Users\\Leno\\Desktop\\JarsPractise\\JarsPrac.jar","com.shiva.practise.FloydTriangle"});
proc.waitFor();
BufferedInputStream is=new BufferedInputStream(proc.getInputStream());
byte[] byt=new byte[is.available()];
is.read(byt,0,byt.length);
System.out.println(new String(byt));
}
}

When I run my jar file nothing happens

I first tried to run my jar file by
java -jar test2.jar
and got the following error
"Failure to load main class Manifest attribute from test2.jar"
after googling stack overflow i came up with a solution saying i needed a manefest file with the nsme of the starting class.
I tried the following
jar cfm app.jar man.txt
Now nothing happens, after I type the line the curser just goes to the next line
man file looks like
Main-Class: cStart
my cstart looks like
public class cStart {
public static void main (String[] args) {
Scanner scanner = new Scanner(System.in);
cRiunTest test;
test=new cRiunTest();
test.run();
if (true)
return;
}
}
Jar command is to manipulate jar (java archive) files. To run a jar you should use the command java -jar program.jar

Categories