Creating terminal output from a JAR file - java

I am creating a program that requires a input/output system, since this program doesn't have GUI, and I am launching it from a JAR file, I was wondering how I can create the input/output system.
Im starting to learn about JAR files, and normally I just run my finished program via a batch script. So how can I get this terminal like input/output system from a JAR? #beginner

As there is no UI, you should run your program from Console (terminal) to use standard input/output. If you are in Window use (.bat) or if you are in linux use shell script(.sh) to call your jar

I don't know if this is what you are asking for, but try this:
java -jar inout.jar
HELLO
gwag
Your input: gwag
faefa
Your input: faefa
feafa
Your input: feafa
faef
Your input: faef
public class Test {
public static void main(String[] args) throws InterruptedException {
System.out.println("HELLO");
Scanner scanner = new Scanner(System.in);
while(scanner.hasNext()) {
System.out.println("Your input: " + scanner.next());
}
}
}

Related

Don't close stdin after initial value was processed in Java

I am making simple console app. My main loop looks like this.
var s = new Scanner(System.in);
while (appShouldRun) {
var action = s.nextLine();
// proccess action
}
It works fine when I run app with empty stdin.
java -jar app.jar
But I want to have some prepared scenarios in file. Run them and then wait for next user input.
Expected behaviour:
java -jar app.jar < scenario.txt
output of commands from file
| <- cursor, waiting for user input
// scenario.txt
cmd1
cmd2
cmd3
.
.
.
Problem is that when I run program with something od std in, it process commands correctly, but then it throws
java.util.NoSuchElementException: No line found
I think it's becouse stdin is closed after all lines from file is processed.
Am I wrong?
How to fix the code to get expected behaviour?
Thanks
When you redirect standard input with <, it is connected to the input file, and not connected at all to the console.
If you want your program to receive input on the console, then you can't redirect stdin. You should take the filename as a command-line argument instead. Open it, read it, close it, and then process stdin.
OR, if you really want to, and you're on a unix/linux/macOS system, you can run it like this, using the cat command to concatenate standard input to something else:
cat scenario.txt - | java -jar app.jar
I have tried do change as little code as possible. This reads the whole file like you wanted and ends without Exception
public class Main {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
boolean appShouldRun = true;
while (appShouldRun) {
String action = s.nextLine();
System.out.println("got: " + action);
appShouldRun = s.hasNext();
}
}
}

Trying to understand how to create a jar file that opens terminal and takes in a argument

I finished creating a program but I was told that my program
must be a Java application that takes as a command line argument the name of the file."
I understand I can use the jar command in terminal but I don't undestand how you open the terminal and take a file name as a argument. I was wondering if someone could explain what code is required to do this.
Thanks alot.
I tried creating a basic jar file in terminal with the line "jar cvf findOptimalTransport.jar ." but the jar file does not open, I think its because the current implementation takes the users input with a scannar in the code and prints via the terminal. However, this wont work because a terminal window is not opened with this command.
It doesn't have to be a jar file. Command line arguments can be entered from the command line, when you run your application.
Let me give you an example, about how this works. Let's say you have the below simple Java application:
public class MyApplication{
public static void main(String[] arguments){
System.out.println("Hello World!");
}
}
That public static void main() is a method; and more specifically the main method of your application which is what is executed when compiled and ran.
To compile and then run it, you type in the command line/terminal:
javac MyApplication.java //this will compile it
java MyApplication //this will run the main method of MyApplication
But what is that parameter in the main method? What is String[] arguments ?
When you run your program, whatever you type after the application name is an argument, of type String and it is stored in the String array String[] arguments (or most commonly String[] args).
What this means, is that, if you execute your application like this:
java MyApplication some_file.txt // Run application with one arg.
You can access that argument like so:
public class MyApplication{
public static void main(String[] arguments){
System.out.println("Hello World!");
System.out.println("You entered: " + arguments[0]);
}
}
Output:
Hello World!
You entered: some_file.txt
Note: To run a jar file, you need to navigate to the folder that the jar file is in and from the command line you can run it by typing:
java -jar <jarname>.jar

Command-Line Arguments Being Lost?

I'm writing a Java program to clean some data.
I'm passing it the files I need, but the first file is ignored!
Minimum code to reproduce the issue:
public class Classifier {
public static void main(String[] args) throws IOException {
System.out.println(args[0]);
for (String s : args) {
System.out.println(s);
}
}
}
I'm running it with the following Command-Line Argument:
java Classifier < March.csv February.csv
And the output I'm receiving is:
February.csv February.csv
Can someone explain why this is?
The < March.csv is being interpreted by the shell as an input redirect. The contents of March.csv are sent to your program's standard input, which you are ignoring. This happens in the shell, before your Java program is even started. So, only February.csv is being sent as a command line argument to main.
Remove that <, so that all command-line arguments you intended to send to main are sent.
< operator redirects this file to stdin to this Java process
so
if you just have
java Classifier < March.csv
and try to read arguments, you would see none and if you read stdin you would read the file content

Consoles in Java

I do have some serious troubles understanding the console in java. I am running Eclipse, and I wanted to write a small program which prompts a few text messages to the console and receives a few strings as input arguments from it. Problem is: When I run my program, it opens the command line window properly, but my outputs are only printed on the Eclipse-Console.
In some way, I do understand why this is the case. The Command Line Windows expects commands, and not just some kind of a string or something. But how do i manage to output my Strings into the Command Line Window and read Strings from it, and not just commands.
Or am I doing it the wrong way? Do I have to open another "Console" where all my messages will be prompted and from which i can read strings a user wrote?
This is the code i use to open a command line window on start:
public static void main(String args[]) throws Exception {
Process process = new ProcessBuilder(new String[] { "cmd", "/C",
"start", "cmd" }).start();
System.out.println(process.waitFor());
Edit: I did still not manage to get this to working. Somehow, when I compiled the program, and I run it, it properly opens a command window, but no messages are posted there. Seems like "System.out.println("xxx") does not have any effect on this window.
There's no "console" specified by your program, but an stdin, stdout and stderr for input, output and error output. When you run your program from windows, these streams are bound to a command window, and if you run it in eclipse, they will be associated to the eclipse console. To give a more obscure example, ff you were running it through ssh, the streams would be associated to ssh, and ssh associated to your command window, and so on.
So, you're not doing anything wrong, you just need to run the program from the command line if you want stdout and stdin to come from that command window.
How do you open a command window, by the way?
You might want to read through this page:
http://pages.cs.wisc.edu/~hasti/cs368/JavaTutorial/NOTES/JavaIO_Scanner.html
Basically what you need to is create the input stream, tell the user to input something, and then get the input. E.g.
private static Scanner newScanner = new Scanner(System.in);
public static void main(String[] args) {
System.out.println("Put your input here: ");
String inputValue = newScanner.nextLine();
System.out.println(inputValue);
}
Just remember to import the scanner library!

run class file as separate process from java code

public static void main(String args[]) throws IOException
{
Process p = Runtime.getRuntime().exec("java E:/workspace/JNIProgram/src/JNIProgram.class");
}
so I have this code and am trying to run the JNIProgram.class file however the program gets terminated instantly without doing its job (which is to create a new txt file and write to it)
So what am I doing wrong
The java command expects a Java class name, not a filename.
So the command java E:/workspace/JNIProgram/src/JNIProgram.class is wrong. If you try this manually from a command prompt window you'll get an error message.
The command should be something like this:
java -cp E:\workspace\JNIProgram\src JNIProgram
Note: What's after the -cp option is the classpath, and after that the fully-qualified class name (which is just JNIProgram, if the class is not in a package).
First make sure that you can run the command manually from the command line before you make it work from another Java program.

Categories

Resources