1

I have a java Program which accepts inputs and prints outputs based on the input provided by the user. For example, when I run it, the program prompts the user:

System Starts  

login <--- User input

Login Successful 

I want to be able to run the program through bash and save the output of the program to a text file.

This is what I tried:

echo "login" | javac TicketSystem/src/*.java > Output.txt

But when I do this, my Output.txt file remains empty. I want the Output.txt file to contain:

Login Successful

Is this possible?

3
  • Do you see "Login Successful" on the screen when you run that? And is javac how you run it when it prompts you for login normally? Commented Nov 4, 2015 at 21:11
  • @EtanReisner I'm not actually sure if I'm supposed to use javac or not, I thought that's what I need to do to run the java program. When I run the program using javac however, I don't see anything on the screen, that is only what is shown in Eclipse Commented Nov 4, 2015 at 21:14
  • That's because javac is a java compiler not a java runner. Commented Nov 4, 2015 at 21:20

2 Answers 2

3

To run a java program, you must use java and specify the name of the class containing your main method.

Before you can run it, you must ofcourse first compile the .java source file into a .class file.

For instance you have Foo.java containing a public static void main(String[] args).

  1. compile it: javac Foo.java --> this gives you a Foo.class
  2. run it: java Foo --> this takes Foo.class and executes it

I suggest you try to compile and run the program without the redirection first, then if that works, try to get the redirection working.

Sign up to request clarification or add additional context in comments.

4 Comments

TicketSystem is the name of the program that contains multiple classes, how would I use java on a program with multiple classes?
When you say "program" I think you mean "directory". The program consists of the various source files inside the directory. You need to compile all source files, like javac TicketSystem/*.java - Then run (using javac) the one containing the main method. This is actually pretty basic usage - I suggest you read up on java in general first.
This was helpful, but now when I run my main class using java TicketSystem/src/Main (Main being my main class) I get a list of errors java.lang.NoClassDefFoundError and (wrong name Main) ... any thoughts as to why?
You can't just specify the class using the full path like you showed. Provided your .class files are in your current directory, you can probably just say java Main. If they are inside TicketSystem/src you can say java -classpath TicketSystem/src Main
2

try:

    echo "login" | java <class name that contains main method> > Output.txt

javac is the compile command which produces .class files. For instance if I have Program.java that contains a main method, I would first run

    javac Program.java

Which would produce a Program.class file. You would then run

    java Program

to execute the main method the program.

So in this case if your main method was in Program.java, you would run

    echo "login" | java Program > Output.txt

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.