Help me understand what is being asked here. The question instructs me to use the command window to display the following statement about comments:
"Program comments are nonexecuting statements you add to a file for the purpose of documentation."
Also include the same statement in three different comments in the class; each comment should use one of the three different methods of including comments in a Java class.
What is the "command window"? Is it the window that appears when I press run? If so wouldn't comments not display at all.
Yes, depending on your IDE, the command window is the prompt which you can start (or stop the program with), receive output and enter input into the program. If you are using the Windows CMD software, you generally will use javac [filename].java to compile and java [filename] to run the code.
Inside your Java code, each line is run as a meaningful statement causing the program to do something. However comments are used to ignore lines in your program, such as messages for yourself or documentation to note to others who may view the program, how to use it.
Examples of comments are (single line): \\ This line will NOT be run
and (block):
/*
Lots of text can go here
This will not be run at all.
Not even the third line.
*/
However you are asked to display a line and that can be done with the print (or println) command from the out subclass of System class.
This would look like:
System.out.print("Program comments are nonexecuting statements you add to a file for the purpose of documentation.");
And that way the string of text,
Program comments are nonexecuting statements you add to a file for the purpose of documentation.
is printed into the commandline.
Microsoft Windows Command Prompt Window is what is meant there.
http://windows.microsoft.com/en-us/windows-vista/open-a-command-prompt-window
Related
Essentially the question boils down to 'Is there a way to run a class/jar file using the console for input/output without having JDK'. I've written a program in Java for a colleague to use in his work, but he doesn't have/use JDK. I'm aware there are online compilers, but at this point my question has become focused on running a program directly for interest reasons.
Essentially the program is designed to output instructions to the user, the
user makes input in accordance with the instructions, and this goes back and forth for awhile before the program does some calculations based off the input and returns a final result.
I should mention that he has JRE.
My program doesn't use a GUI, it just prints instructions to the console and gathers input from responses typed in the console. It works fine on my computer when running it from the command line ('java myprogram'), but without jdk the java command isn't available to him, which seems to mean he can't run a class file.
My next attempt was to turn the program into an executable jar file, but using the command 'myprogram.jar' from the command line doesn't really do anything. The jar file does include a manifest. I added a blank screen to the code and running the jar file did create the screen, but still no i/o on the console. From what I've read, I think this is because jar files aren't automatically associated with a console, so there is nowhere for the program's output to go or input to come from. He can't use the 'java -jar myprogram.jar' command because he hasn't got the JDK, so even though that command runs the program the way I want it to, it's not an option.
Basically, I'm wondering if it's possible for someone without JDK to run a program and interact with it entirely using the console/command line and no other interface?
I'm a noobie to Java and I had some questions, I think they may be easy (duh) answers to you experts but for me, I couldn't figure out the answer.
What is the point of Java programs, all I am making in my class now are simple read text files, have user input some stuff on the command line and the program prints out something. Like, it's cool and all that I can make a program do this but what does this all lead up to?
Say, I create a java program that converts celcius to fahrenheit and I want to have someone use it. Would I be able to get someone to use it that has no knowledge of Java, that is to say, giving it to my mom and she can run it from her computer without using Eclipse? How do I have someone who has no knowledge of coding run my program?
And my last question is, what is the point of command line arguments? Instead of putting them in the arguments, why not just have it in the code itself? Is it because if the code is big, it might be hard to find it? And if I download code that requires argument input, is it possible to download the code with the arguments set in Eclipse or would I have to manually do it?
Thanks, sorry for the long paragraph, but just wanted to put my thoughts down.
I think the short answer is modularity. Compiling or building a Java program takes time and knowledge (like you mentioned, someone who has no knowledge of Java probably wouldn't know what to do).
Passing in command line arguments allows you to just send someone the binary distribution of your program and passing in their own parameters without having to know what your code looks like or have to modify it.
In real life, programs aren't run from Eclipse (obviously), they're usually packaged into jars or wars and executed from the command line or some kind of app server.
If you download code that requires command line arguments, you will probably have to put them into Eclipse yourself, but hopefully the application has some documentation or help usage that explains what the arguments should look like.
You can create your own jar files that you can run from the console.
One of the simpliest ways to make your program usable without using Eclipse is with the Windows command prompt or Linux terminal:
go to you working directory (where your java file is stored)
Type javac (Example:javac hello.java) -This would compile your program
Type java (Example: java hello) - This will start it
Using of args is when you want to set some information to your program with the launching of the program.
Another way to start your program without using Eclipse is to make your own executable jar. With this you will get something like .exe file From which you can start your aplication
Most of the java programs are started from the CMD / terminal, or runs on java servers. There are aplication which have their own user interface (SWING, SWT, AWT ...) .
Most popular types of Java usage is creating a serious bussiness software, android aplications, complex web aplications etc.
what is the point of command line arguments?
Benefits of using Command line.
if I download code that requires argument input, is it possible to
download the code with the arguments set in Eclipse or would I have to
manually do it?
Right Click on project-name > Run > Run Configuration > select Arguments tab
For more info click here.
What is the point of Java programs, all I am making in my class now
are simple read text files, have user input some stuff on the command
line and the program prints out something. Like, it's cool and all
that I can make a program do this but what does this all lead up to?
This is just the begining. Learn Java core. Get familiarized with Java. One day you will be building tools using Java. eg:- Apache
How do I have someone who has no knowledge of coding run my program?
Compiling a java program into an executable
Before you do any this, get a book on Java (my fav - Java The Complete Reference) and read it. Learn how Java works, why is it different from other languages, best practices etc...
After 2-3 years from now, you'll get the importance of taking a small step by building simple cmd line programs using Java
I want to implement a command line interface within the Java GUI window. Is there a good way of doing this or an existing project/example I could use?
My application visualises a graph in the GUI and I would like to enter verbal commands for editing it, instead of developing a point-and-click interface for every possible command. It would be similar to a normal command line but I would have a custom parser that analyses the entered command strings.
You can write your command and press enter, upon which the string is sent to a listener for parsing and execution.
Show a scrollable history of previous commands.
After parsing I might want to print something onto the screen (which would go to the history area).
Perhaps a command-line-style prefix (">").
Ability to edit the current command (backspace) before submitting it (enter). Not being able to delete the prefix or previous commands in the history.
Up and down keys would allow navigating and repeating previous commands.
You have the Groovy Console. See the paragraph Embedding the Console for your app swing.
I have a peculiar problem. I have a Java program that is run with the command :
cat input_file_name | ./script_name > file_output_name
the script_name script just does : java myprogram
My question is : how can I print out something in the console without it being "put in the file_output_name file" (since the > file puts all System.out.prints in that file)
I know this is possible because there are some already that come from some class in a library that I'm using from my java program. However I can't find the exact source of those prints so I don't know how it is coded.
Thank you for any help.
The simple answer is to write those messages to System.err rather than System.out.
This will work since > redirects standard output but not standard error. (You can redirect standard error, but the shell syntax is different; e.g. foo 2> /tmp/somefile.)
A more complicated alternative is to change your program so that it can be called with the name of the output file as an argument. Then open the file, wrap it in a PrintWriter and write to that stream rather than System.out. With a bit of refactoring, you should be able to structure your program so that it can be used either way.
The easiest way is to use System.err.println() instead of System.out.
It will go to a different "device" (stderr instead of stdout), and it won't be redirected.
With what you've shown, you're only redirecting the standard out (stdout). You can write something to the standard error (stderr) instead to have it show on the console yet. In Java, this can be done by System.err.println(...), and related methods.
Notice that if a Java program writes to standard output, it can't control where does its output get redirected, that depends on how the program is invoked from the command line.
Having that clear, you can redirect the program's standard output only to the console, by simply removing the > file_output_name part from the command.
Or, you can redirect the output of the program to both the console and a file by using the tee command, take a look at this article which explains in detail how to do it.
I would just like to know whether it is possible to make a command prompt in Java.
My friend asked to make it, I wanted to know if it was possible or not. If it is possible, can someone suggest me some api or something? Thank you.
EDIT: I want to make it similar to windows command prompt
EDIT 2: I would like to make a SWING GUI application and put a command prompt inside of it.
Yes. Use the Process API.
You can run commands in Java using the Process API. You can also get the output and write input to the runned process. For more info, see this tutorial.
But if you want to make a terminal emulator (such as those in Linux) in Java,
I recommend having a look at JCTerm or JTA.
You must be careful how you start it.
If you start your program with java.exe then the console (input/output) is shown. With System.out.println("mymessage"); you can print (output) text to the console. With System.in you can read from the console. This delegates to the java.io.Console class (available throug System.console()).
If you start your program with javaw.exe, then you don't see the console. You must then create your own screen to allow input/output. This is the default on Windows.
Java can do console I/O and it can launch processes, so yes, it's possible. You'd use methods of System.in and System.out to display a prompt and read commands, and a ProcessBuilder to execute programs.
yes it's possible in java
your have to do some research on Java.lang & IO
Check the class java.lang.Runtime. It provides a couple of exec() methods.