I was developing a simple C code generator in java in Linux and i wanted my java program to automatically compile and run the generated C code,i.e. the .out file. Though i have been able to compile it successfully I am not able to run the compiled object code. Can anyone please write the code to suggest how to execute the C code using the java program.
If you're successfully creating the .out file, then you should be able to run it with one of the Runtime#exec functions:
Runtime.getRuntime().exec("./a.out");
...or (more control) via the stuff in the Process class. The Process class stuff lets you do things like control the input and output (via streams).
Related
Is there a way to just run individual java commands without having to create a file? Kind of like the matlab command window, where you can just type one line and it'll run.
Currently
No.
Java is not a terminal programming language like matlab and python.
To run java applications, the commands must be compiled
When you compile, the java compiler turns the code into a class file. The class file contains "instructions" for the computer which is the application.
To clarify, Java 9 has JShell, which is a REPL for java. You can find the early access here.
I think what you are looking for is something like this
https://blogs.oracle.com/java/jshell-and-repl-in-java-9
Sadly it isn't available until java 9, so in its current state it might not be production ready
try is here https://jdk9.java.net/download/
this shows you how to get it going
http://www.journaldev.com/9879/java9-install-and-repl-jshell-tutorial
Here's a Progress program that creates a record in the Symix database:
create audit.
assign audit.table_name = "JavaSample"
audit.key_id = "12345"
audit.field_name = "<FieldName>"
audit.audit_dt = today
audit.audit_tm = time
audit.audit_user_id = "javauser".
I want to call this .p file from the java code.
Progress offers Open Client runtime package to call .p through AppServer.
It is required to generate java classes from compiled .r file using ProxyGen from Progress OpenEdge Studio installation, then put those generated classes put into Java project.
But this variant is complicated and not easy to use, especially if parameters changes frequently.
Alternative way to ProxyGen is to use opa library. It simplifies Progress .p procedures call from Java.
All you need is this case - it is to create a simple parameters object and to call runProc method. Parameters will be mapped on the fly.
Of course, you still require an AppServer on Progress side to run those .p.
More info in https://github.com/labai/opa
When using the AppServer you can run the .p file on the AppServer using the OpenClient proxies for Java:
http://documentation.progress.com/output/OpenEdge116/pdfs/dvjav/dvjav.pdf
You could use Runtime to execute a shell script, as covered in for example this question: How to run Unix shell script from Java code?
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.
i have this problem:
I have created a bash script that performs some tasks. At one point this script call a java program. This java program perform some wsdl tasks and in one point have to call an external fortran program that perform a simulation and put the outcomes inside a file "oucomes.dat". program.exe is perfectly executed but the java program seems unable to open the file created by the fortran program. The code in the java program that call the fortran simulation is:
Runtime.getRuntime().exec("./script.sh");
where script.sh contains
#!/bin/bash
./program.exe
When i call first program.exe and then the java program the java program can read prefectly "outcomes.dat". The problem is that i have to call program.exe from inside of java because i need some data in realtime from a wsdl service and eventually send data back to the wsdl service. So i guess that the problem is in the form that i call program.exe from inside the java. One solution may be to split the code in java in two program and putting between the to program a call to program.exe. But i would like a faster solution (in terms of CPU and memory usage). Which is the correct form to call the program.exe in order to allow the java program to read the "outcome.dat"?
PS: i use linux.
It doesn't look as though you are waiting for script.sh to finish. You need to do something like this (untested):
Process p = Runtime.getRuntime().exec("./script.sh");
p.waitFor();
You can also test the process's exit value using exitValue().
See http://docs.oracle.com/javase/6/docs/api/java/lang/Process.html .
I want to create a program using Java for Automatically copied USB's data when it's insert to machine. How I do it?
There is no such thing as "USBs data", the very concept doesn't exist.
There is nothing specific in Java SE for do this job.
I may think of two ways to get that working:
Write a Java program that starts on boot (maybe a service), the prog scans continously available "drives" (D:,E:,F: ... in Windows, mount on Linux), the USB flash may be marked with a specific folder/file name (eg. COPY_USB_). That can be done with the File class.
Write a Java program that get invoked on plug-in. I know that can be done on Linux with hotplug-script support.