We are building a grading system, and part of its job is to take input files, and from the given directory, our system will compile and run a (non-java) source code file written by the students. Then, the system will display the output from that run.
It is not limited to python, any other language that can meet the requirement is OK.
Is there is any method for providing for the location of the python code and the input file, and then run that code, returning the output file. If not, how can i achieve this goal?
Take a look at the Runtime class. In particular, look at Runtime.exec(). It should enable you to execute external applications from within your Java program, as well as passing command line arguments, and specifying working directory.
Note that the python or any other program must have some well-defined way to get its input and write its output, such as passing filenames on the command line, or reading from stdin.
Related
For one of our projects for school, we're asked to create a command-line executable program representing a hypothetical school grades system for teachers. We can use any language we want and I chose to use Java. However, with a Jar file executable, I'm not able to freely use the command line in Linux to use the methods I've created. It only runs through the commands I've written in the Main Java file and then exits. I suppose I can run a scanner but that seems tedious to scan and pick out my variable arguments to pass into methods.
Am I missing something? Or did I make the wrong language choice?
Thanks!
You can make an indefinite loop in your main which will get the input from console using BufferedReader (recommended) or Scanner.
Then, you could use a Scanner to tokenize de input and check whether the input corresponds to a command using a switch/case.
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'm very new to all Java related programming. For a school assignment, I've created my Java application using BlueJ. Apparently, the application should be able to run from the command prompt with the following line:
myapp -compress fileName
Honestly, I don't have the slightest idea about how to setup such:
My application has a Main class. Am I supposed to change it to be called myapp?
I've been running my app with java Main compress filename. I see that now I shouldn't be using the java key. But of course, as it is now, it won't work if I remove it. How can I run the app without it?
Is there a difference between having the compress argument I always use and the -compress one they tell me? Is that dash (-) any special?
Looking at this page: http://docs.oracle.com/javase/1.4.2/docs/tooldocs/windows/java.html, it seems to insist that to run my program I do need to use the java key. And the dash seems to be used for something called options - there are standard and non-standard. However, there doesn't seem a way to make a "custom" one (-compress).
So my question is, how can I run my application with the above format?
The easiest way it to create a one-line shell script (if you're using Unix) or a one-line batch file (if you're using Windows). Call it myapp (or myapp.bat) and make it launch Java, passing the appropriate arguments.
As to the -compress argument, your main() takes an argv. You'll need to examine that to figure out what arguments have been passed to your program. You can either code everything yourself (very easy in your case), or use an existing framework:
How to parse command line arguments in Java?
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.