Issues running Gradle program from terminal - java

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

Related

IntelliJ Idea - Cannot find input file(bank.in) when running through IntelliJ

I have a homework and I did it on Linux, Visual Studio Code, and the command line. It was working perfectly fine until I need to debug my code. So I migrated to Windows 10 because I had IntelliJ IDEA installed there. I compiled the code and place the input file "bank.in" in the same folder as the compiled "MyClass.class"
However, when I run the program from IntelliJ, my code catches the exception that it cannot find the file "bank.in" when it is just in the same folder as "MyClass.class".
My method in creating the bank.in was, right clicking the out folder from IntelliJ and adding a new file and adding the bank.in contents from there
I've tried running it through cmd.exe using java MyClass and it works perfectly. No exceptions are caught.
But when run through IntelliJ IDEA, it shows
Cannot find bank.in...
Exiting the program...
This is the part of my code where I input my file.
public void main(String[] args)
{
String fileName = "bank.in";
FileReader bank = null;
BufferedReader bankBuffered = null;
try
{
bank = new FileReader(fileName);
bankBuffered = new BufferedReader(bank);
}
catch(FileNotFoundException f)
{
System.out.printf("%s is not found.%n%n", fileName);
System.out.printf("Exiting the program...%n");
System.exit(0);
}
}
This is my project folder structure
MyProject
-.idea
-encodings.xml
-misc.xml
-modules.xml
-workspace.xml
-out
-MyClass.class
-bank.in
-src
-MyClass.java
When I run it through cmd.exe, it works fine. Is there any workaround through this? Thank you.

So i just started learning java in treehouse.com ive done the basics but when i try everything ive learnt there on the intellij idea ide nothing works

So I just started learning java in treehouse.com I've done the basics but when i try everything I've learnt there on the intellij idea ide nothing works.
I've already searched the internet trying to find a solution and I still cant find it, yea I've tried system.out but I've seen other people do it with just typing console too and if I try system.out on readline it doesn't work I'm literally confused.
I'm used to using the
console.println("Hello world!");
and
String name=console.readln("whats yo name: ");
and stuff like that on the treehouse workspace but everytime i try to do it on intellij idea it keeps getting an error.
it looks like this
package com.company;
import java.io.Console;
public class Main
{
public static void main(String[] args)
{
console.println("dada");
}
}
Console console = new Console()
This code won't work on IntelliJ.
To take input in an IDE use the following code.
Scanner sc = new Scanner(System.in)
String in = sc.nextLine();
System.out.println(in);
You can also use the older way,
BufferedReader br = new BufferedReader(new InputStreamReader(System.in)));
String in = br.readLine();

Java - Command Execution in Runtime

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"};

unable to use taskkill.exe from a java process

I need to kill an external process on windows (WindowsXP 32bit) from my integration test. I thought I'd just use 'taskkill.exe' but I cannot seem to get it working. Basically, every time I kick off a 'taskkill.exe' process from java it returns exit value -1073741515, nothing is printed to std error/output.
To reproduce the problem I wrote this simple application:
public static void main(String[] args) throws Exception {
ProcessBuilder builder = new ProcessBuilder();
//In my real code, I kill process by its pid. However below also shows the problem:
builder.command("taskkill.exe", "/?");
builder.redirectErrorStream(true);
Process p = builder.start();
BufferedReader r = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line = r.readLine();
System.out.println("out:");
while(line != null) {
System.out.println(line);
line = r.readLine();
}
System.out.println(p.waitFor());
}
More data points:
-1073741515 apparently means "The application failed to initialize properly". Not very helpful for me though ;)
I've tried bunch of combinations of taskkill.exe parameters; I've tried prefixing the command with 'cmd', '/c'. Symptoms are exactly the same
I tried executing other windows programs that live under windows\system32 and I also get -10737...
Executing things like 'dir' or 'echo' works ok.
Any hints on what might be the problem?
Have you tried executing your application as a different user? If you're running your app with a plain batch file in windows, right click and select Run as administrator and see the results. It's likely the account you're running under doesn't have enough rights to execute native apps.

Keep JVM running with a JAR from the command line

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

Categories