I'm running the closure templates compiler on a soy file using a watch - it just runs
java -jar SoyToJsSrcCompiler.jar --outputPathFormat simple.js simple.soy
every time the file changes.
The problem is that it takes a long time for the jar to load up each for each run. Is there an easy way (easy=command line tool) that would keep the JVM running to make each run faster?
Checkout Nailgun
http://www.martiansoftware.com/nailgun/index.html
You can create a simple class that calls SoyToJsSrcCompiler's main method passing the args taken from System.in.
Something like this (not tested) (enter "quit" to exit the application):
public static void main(String args[]) throws Exception
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
while (true)
{
System.out.println("Enter command (eg: --outputPathFormat simple.js simple.soy): ");
String line = br.readLine();
if (line.equals("quit"))
break;
com.google.template.soy.SoyToJsSrcCompiler.main(line.split(" +"));
}
}
And execute your launcher withouth arguments:
java -cp MyLauncher.jar:SoyToJsSrcCompiler.jar launcher.MyLauncher
Related
When i run the code inside IntelliJ IDEA it works perfectly but if i try running it from terminal with "gradlew run" it prints the question but doesn't give me the chance to type anything. It immediately prints "Hello null by Gradle" and finishes the run.
Does anyone know why it does like this?
public static void main(String[] args) throws IOException {
System.out.println("What's your name?");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String name = br.readLine();
System.out.println("Hello " + name + " By Gradle");
br.close();
}
I create this project with the task "gradle init" i chose application, Java, Groovy, JUnit Juppiter.
Two actions can help you to get this resolved;
Add below task in your build.gradle.
run{
standardInput = System.in
}
Though this enables your command-line to accept input now. It still provides some ambiguity in run and hence executing below command can help you to get your exact expected output.
gradle --console=plain run
Detailed explanation wonderfully explained in console-application-with-java-and-gradle
The following method starts the cmd in Windows and it takes a parameter of the command which need to be run.
I have tested this method using the following commands: net users and it worked fine and it printed the users accounts. but if I run the dir command I get the following error:
java.io.IOEXception:
Cannot run program "dir": CreateProcess error=2, The system cannot find the file specified (in java.lang.ProcessBuilder)
Code :
private String commandOutPut;
public void startCommandLine(String s) throws IOException{
Runtime runtime = Runtime.getRuntime();
Process process = runtime.exec(s); // you might need the full path
InputStream is = process.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String commandOutPut;
while ((commandOutPut = br.readLine()) != null) {
this.commandOutPut = this.commandOutPut + "\n" + commandOutPut;
}
System.out.println(this.commandOutPut);
}
Well, obviously, your method does not start cmd. How did you get this notion?
The net command is a standalone command so it runs just fine, but the dir command is not standalone, it is an internal command of cmd.exe, so you cannot run it without launching cmd.exe to execute it.
To get it to work you will have to pass not dir but cmd.exe /c dir or something like that.
Don't know if this perception can help you. But, seems that "net users" are recognized as Windows command, since "Execute" dialog can run it.
But, for some reason, the "dir" command aren't. When try to run, Windows responds that command was not found.
Additionaly, I tried run Command with inline arguments too, but the arguments are simply ignored. (sorry for bad english)
My best guess is that this is because "net" is a real executable (there is a file WINDIR\System32\net.exe"), while "dir" is a builtin command of the command interpreter - it has no executable and is directly executed within cmd.exe.
Howevever you may get around this be invoking "dir" command inside the cmd process. The syntax - as per Microsoft docs - is:
cmd /c dir
There are also some related answers on the site:
How to execute cmd commands via Java
Run cmd commands through java
You can use the following code for this
import java.io.*;
public class demo
{
public static void main(String args[])
{
try
{
Process pro=Runtime.getRuntime().exec("cmd /c dir");
pro.waitFor();
BufferedReader redr=new BufferedReader(
new InputStreamReader(pro.getInputStream())
);
String ln;
while((ln = redr.readLine()) != null)
{
System.out.println(ln);
}
}
catch(Exception e) {}
System.out.println("Done");
}
}
I am indebted always.
How can I do to start the external program of java, but it does not work.
In the following program, although I will work if started from the command line,but,It does not work when i run in eclipse on.
I am out error that appears is, and can not load the main class is missing or(エラー:メインクラスがみつからないかロードできません).
How will be able to run an external program in eclipse on what if?
Please kindly advise Thank you.
package test.jersey.resources;
public class Execute02 {
public static void main(String[] args) {
String filename = "C:\\Users\\omiz12032\\workspace3\\TestJavaServlet\\src\\test\\jersey\\resources\\start.bat";
try {
Process proc = Runtime.getRuntime().exec(filename);
System.out.println("実行中");
proc.waitFor();
System.out.println("実行終了");
} catch( Exception e ) {
System.out.println(e);
}
}
}
You're calling Runtime.exec(String) which delegates to Runtime.exec(String[], String[], File) in two steps, result in nulls for second and third parameters.
Those are envp and dir. Inheriting envp (aka Environment Variables) from your Java process shouldn't be a problem. The bigger problem is dir which is the working directory. If the .bat file you're calling is not well-written (not ready for being executed outside its directory) it'll fail to resolve its dependencies when using your program's working directory. Try the following:
File workingDir = new File("C:\\Users\\omiz12032\\workspace3\\TestJavaServlet\\src\\test\\jersey\\resources");
// the no-array call of exec tokenizes your command which may lead to unexpected results if it contains spaces
// also you can put arguments here as well as separate array elements
String[] command = { "start.bat" }; // for example { "start.bat", "--fooBar" }
Process proc = Runtime.getRuntime().exec(command, null, workingDir);
I have been trying to get this program to run a class file that is located in /Library/Application Support/Adobe/Adobe PCD/cache/Main.class.
Yet every time I run the method below, It cannot find the file specified. I think that eclipse is looking for the file path above in the project folder. But the first command doesn't seem to work. Can Anyone help me? Is there another way to run the class file?
static Runtime r = Runtime.getRuntime();
public static void Run() {
try {
Runtime rt = Runtime.getRuntime();
String run = "/Library/Application Support/Adobe/Adobe PCD/cache/";
String[] command = {"java -cp " + run, "java Main"};
Process pr = rt.exec(command);
BufferedReader input = new BufferedReader(new InputStreamReader(pr.getInputStream()));
String line=null;
while((line=input.readLine()) != null) {
System.out.println(line);
}
int exitVal = pr.waitFor();
System.out.println("Exited with error code "+exitVal);
} catch(Exception e) {
System.out.println(e.toString());
e.printStackTrace();
}
}
This tries to run a mythical program java -cp /Library/Application Support/Adobe/Adobe PCD/cache/ with argument java Main. java Main is an illegal name for a class too. Instead, have your command array be:
String[] command = {"java", "-cp", run, "Main"};
However, unless you have a Main class in the default package, simply add the run directory to your application's class path, and then call Main.main(), possibly playing games with standard input and output. There is no need to start a second JVM. Or, figure out what Main does, and call the classes it calls. Is there Javadoc for the Adobe code?
When the Ant task <zip> runs, Ant does not start a new VM or a command-line zip utility. Instead, the task's class calls methods in java.util.zip. You should do something similar.
I tried out a simple program to execute Linux command at run time. But the following program gets compiled and runs without any error, but the text file is not getting created as intended.Is there anything wrong in this program?
import java.io.*;
class ExecuteJava
{
public static void main(String args[])
{
String historycmd = "cat ~/.bash_history >> Documents/history.txt";
try
{
Runtime runtime = Runtime.getRuntime();
Process proc = runtime.exec(historycmd);
}
catch(Exception e)
{
System.out.println(e);
}
}
}
Try accessing some of the functions Process provides. I'd start with exitValue. Typically a -1 indicates something went wrong while a 0 means nothing especially bad happened.
Also try InputStream and Error Stream, and read them fully. See if either has useful feedback for you.
Other than that, try what andy256 suggests in comments. Ensure the Documents directory exists in the executing directory of the program.
The append operator >> is meant to be interpreted as part of the command shell. Use
String[] historycmd =
{ "bash", "-c", "cat ~/.bash_history >> Documents/history.txt"};