Run Non GUI Jar Files - java

I know that I can run non GUI jar files from the command line. Is there any way that can do so by clicking or something and not writing the commands again and again.? Is there any software to do so. ( I am talking about a compiled jar and don't want to run from any ide)

public static final String TITLE = "CONSOLE title";
public static final String FILENAME = "myjar.jar";
public static void main(String[] args) throws InterruptedException, IOException {
if(args.length==0 || !args[args.length-1].equals("terminal")) {
String[] command;
if(System.getProperty("os.name").toLowerCase().contains("win")) {
command = new String[]{"cmd", "/c", "start \"title \\\""+TITLE+"\\\" & java -jar \\\""+new File(FILENAME).getAbsolutePath()+"\\\" terminal\""};
} else {
command =new String[]{"sh", "-c", "gnome-terminal -t \""+TITLE+"\" -x sh -c \"java -jar \\\""+new File(FILENAME).getAbsolutePath()+"\\\" terminal\""};
}
try {
Process p = Runtime.getRuntime().exec(command);
p.waitFor();
} catch(Throwable t){
t.printStackTrace();
}
} else {
//THERE IS YOUR CONSOLE PROGRAM:
System.out.println("Hey! What's your name?");
String read = new BufferedReader(new InputStreamReader(System.in)).readLine();
System.out.println("Hey, "+read+"!");
Thread.sleep(10000);
}
}
You can run it with double clicking on .jar file. Don't forget about MANIFEST.MF! :) (working on linux, also!)
Example (I only double clicked on jar file):

The way intended by Java is that you call java -jar XXXX.jar on the jars you need. Drawback is that you can't specify a classpath so all classes should be there.
A cooler way to package an application is by using Java WebStart. With that the user installs the application jut by clicking on a web browser. Check here http://docs.oracle.com/javase/8/docs/technotes/guides/javaws/developersguide/contents.html

Related

How does java run batch file in the background

How does java run a batch file in the background?
I have a batch file with the following content
#echo off
:start
start /b cmd /c example.exe -c config >nul 2>&1
goto :eof
In fact, I run this code under the command line, it can run in the background, but it seems that the "execution" of the java call will hang on the console on the idea, I am a java novice and I don't know the principle.
java code:
package com.example;
import org.apache.commons.exec.CommandLine;
import org.apache.commons.exec.DefaultExecutor;
public class Example {
public static void main(String[] args) throws Exception{
System.out.println("Hello World");
System.out.println("Started.");
String script_path = "";
boolean isWindows = System.getProperty("os.name").toLowerCase().startsWith("windows");
System.out.println(isWindows);
if (isWindows) {
System.out.println("it's Windows");
script_path = "c:/users/test-running/desktop/example.bat";
CommandLine cmdLine = new CommandLine("cmd");
cmdLine.addArgument(script_path);
cmdLine.addArgument("start").addArgument("server");
DefaultExecutor executor = new DefaultExecutor();
executor.execute(cmdLine);
System.out.println("Finished.");
} else {
System.out.println("it's Linux");
script_path = "/Users/seth/Downloads/example.sh";
CommandLine cmdLine = new CommandLine("/bin/bash");
cmdLine.addArgument(script_path);
cmdLine.addArgument("start").addArgument("server");
DefaultExecutor executor = new DefaultExecutor();
executor.execute(cmdLine);
System.out.println("Finished.");
}
}
}
So does anyone know what java is doing? Or is the syntax of the batch file wrong?

ADB PULL Command

I am trying to use adb for working with the files in my device from a MAC PC.
One of my folders in my phone has a space in it, so I looked around and tried using escape i.e \ and also using quotes with as given below
import java.io.*;
public class TestModule {
public static void main(String[] args) throws IOException,InterruptedException {
String line = "null";
String cmd = "adb pull /storage/sdcard1/Android/data/files/test\\ Information/ /Users/sbc/Desktop";
Runtime run = Runtime.getRuntime();
Process pr = run.exec(cmd);
BufferedReader buf = new BufferedReader(new InputStreamReader(pr.getInputStream()));
while ((line=buf.readLine())!=null) {
System.out.println(line);
}
}
}
I ran the same command in terminal I can access the file but through java it gives me remote object does not exist error.
Any help is very much appreciated
I found a workaround for my problem, posting code below,
import java.io.*;
public class TestModule {
public static void main(String[] args) throws IOException,InterruptedException {
String line = "null";
String cmd = "adb pull /storage/sdcard1/Android/data/files /Users/sbc/Desktop";
Runtime run = Runtime.getRuntime();
Process pr = run.exec(cmd);
BufferedReader buf = new BufferedReader(new InputStreamReader(pr.getInputStream()));
while ((line=buf.readLine())!=null) {
System.out.println(line);
}
}
}
In place of accessing the folder with the space, I accessed it's parent folder
/storage/sdcard1/Android/data/files/test\ Information/<--current folder with space
/storage/sdcard1/Android/data/files<--Parent Folder.
Now adb downloads all the contents of "Parent Folder"

Open a URL in Chrome using Java in Linux and MAC

How to start chrome using Java?
For Windows the code is just as simple as below.
Runtime.getRuntime().exec(new String[]{"cmd", "/c", "start chrome http://localhost:8080"});
Is there something like above?
In Linux you can use like this:
Runtime.getRuntime().exec(new String[]{"bash", "-c", "/path/to/chrome http://yourwebsite.com"});
Replace the /path/to/chrome with the path in your system. Generally Google Chrome is installed at /opt/google/chrome/chrome
Or you can use google-chrome like this:
Runtime.getRuntime().exec(new String[]{"bash", "-c", "google-chrome http://yourwebsite.com"});
If you want to open up in chromium browser in Linux use it like this:
Runtime.getRuntime().exec(new String[]{"bash", "-c", "chromium-browser http://yourwebsite.com"});
For MAC OS try like this:
Runtime.getRuntime().exec(new String[]{"/usr/bin/open", "-a", "/Applications/Google Chrome.app", "http://yourwebsite.com/"});
You can use Selenium.
import org.openqa.selenium.chrome.ChromeDriver;
public class App
{
public static void main(String[] args) throws Throwable
{
ChromeDriver driver = new ChromeDriver();
System.setProperty("webdriver.chrome.driver", "/usr/bin/google-chrome");
// And now use this to visit Google
driver.get("http://www.google.com");
}
this should work fine for ubuntu 15.10 or higher
String[] cmd = {"bash","-c","google-chrome www.yourUrl.com"};
Process p = Runtime.getRuntime().exec(cmd);
Hope it helps.
>>> Check here (Note: Java File is Compiled and run by commands on Terminal)
File name: Test.java
public class Test {
public static void main(String[] args) {
try {
System.out.println("Executing command to open a chrome tab with terminal command!");
Runtime.getRuntime().exec(new String[]{"bash", "-c", "google-chrome https://stackoverflow.com/"});
System.out.println("A New tab or window should get opened in your Chrome Browser with Stack Overflow website!");
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
/**here
google-chrome https://stackoverflow.com/ -> terminal command to open a new chrome tab or window with the stack-overflow website.
**/

How to pass system properties to self-contained Java applications on the command line

I have a self-contained Java application packaged with the javapackager tool (version 8.0, Windows). How do I pass it system property values at application runtime (not at package time) on the command line?
The doc does not seem to address this.
I tried the standard Java way as in:
mypackagedapp.exe -Dmyprop=myvalue
but that does not appear to have an effect.
Here is a code that validates if an argument exists in the command line.
See if the next code can help you.
public static void main(final String[] args) throws Exception {
CommandLine line = validateArgs(args);
if (null == line) {
return;
}
}
private static CommandLine validateArgs(String[] args) {
Options flags = getArgs();
CommandLineParser parser = new BasicParser();
CommandLine line = null;
try {
// parse the command line arguments
line = parser.parse(flags, args);
if (line == null) {
return null;
}
} catch (ParseException exp) {
System.out.println(exp.getMessage());
}
return line;
}
static Options getArgs() {
Options flags = new Options();
Option dmyprop = OptionBuilder.withArgName("dmyprop")
.hasArg()
.withDescription("add description")
.create("Dmyprop");
flags.addOption(dmyprop);
return flags;
}
In order to get environment variable you need to use:
String env = System.getenv(option);
where option is your desired environment variable.
Hope it helped.

Running java program from another java program

I'm trying to run a Java program from another Java application. Here is my code:
public class Main {
public static int Exec() throws IOException {
Process p = Runtime.getRuntime().exec("javac -d C:/Users/Dinara/Desktop/D/bin "
+ "C:/Users/Dinara/Desktop/D/src/test.java");
Process p1 = Runtime.getRuntime().exec("java -classpath C:/Users/Dinara/Desktop/D/bin test");
return 0;
}
public static void main(String[] args) throws IOException {
Exec();
}
}
javac works fine and creates test.class file in bin directory. However java -classpath C:/Users/Dinara/Desktop/D/bin test does not run the test.class file.
the content of the test.java:
import java.io.*;
class test {
public static void main(String args[]) {
try {
FileWriter fstream = new FileWriter("out.txt");
BufferedWriter out = new BufferedWriter(fstream);
out.write("Hello Java");
out.close();
} catch (Exception e) {
System.err.println("Error: " + e.getMessage());
}
}
}
I suppose that something wrong with recognizing Java command. Could you please give me a sample code for fixing this problem or share idea? I'm using Netbeans to run Main class and the location of the application folder is C:\Users\Dinara\Main
Use
System.getProperty("java.home") + "/bin/java -classpath C:/Users/Dinara/Desktop/D/bin test"
instead of
"java -classpath C:/Users/Dinara/Desktop/D/bin test"
You need to supply the full path to the javac, exec won't use the ath to find it for you

Categories

Resources