I have two files int the directory '/tmp': test.txt ,test.pl
the content of test.txt is :
abcdefg
the content of the test.pl is:
#!/usr/bin/perl -w
chdir '/tmp';
$data = `more test.txt`;
open (MYFILE,">","newtest.txt") || die ("can not open this file");
print MYFILE $data;
Then in java class I write :
Process ps1= Runtime.getRuntime().exec("perl /tmp/test.pl ");
Then the content of the newtest.txt generated from the perl is :
:::::::::::
test.txt
:::::::::::
abcdefg
Here is the problem ,There is a difference
:::::::
test.txt
:::::::
but when I run 'perl test.pl' in linux , there is no difference between two files.
anyone knows the reason ? Thanks !
more is the guilty one here. It's intended for looking at a file one page at a time and waits for hitting e.g. SPACE to show the next page. Try cat instead of more to see if that is the reason.
more tries to be smart and autodetect whether it was called in an interactive situation but sometimes it fails. Depending on its settings it might behave as cat then, but you cannot be sure...
Related
I have a Java file Animal.java, and I want to be able to write a terminal command like so:
java Animal -a -print < data.txt
I know that the -a and -print appear as variables in the array arg (which is an input to the main method -- so arg[0] is -a and arg[1] is -print), but how can I access the data from data.txt?
It's is not possible in java, but instead you may pass the location of the file(not required if its already known and fixed) and then access the file's data using InputStream/ readLine() etc..
You can not manipulate the files in the same way that the Unix pipes do. You need treat your arguments so that there is a prefix or a position that defines the name of the file you want to read, so it is possible read the file passed as argument.
// Treating arguments and verify the possibility of reading file
// Get index which is the file name
FileInputStream fstream = new FileInputStream (argv [FileIndex]);
// Handle the file .
See an example here: Passing a file as a command line argument and reading its lines
Imagina the structure src/mypack/Main.java, run on terminal:
$ cd src/mypack/
/src/mypack$ javac Main.java
/src/mypack$ cd ..
/src$ java mypack.Main < path/your/file
I'm creating an xml-file in java using jaxb and XmlStreamWriter. This will become a very large file and has to be split into several pieces of max. 200MB. These pieces shouldn't be readable xml anymore.
The name of this file is very specific using the date and several parameters and at the end they're numbered like this: '3.1', '3.2', '3.3' where the first number is the number of chunks created and the second number is the following-number of the file. The first part of the filename (apart from the numbering) is created in the java application.
Now I want to create a UNIX script that calls the java application with the needed parameters, splits the file and renames the chunks.
I know the commands to call the java application and to split and rename files but I don't know how to combine them because I only now the filename in the Java application so I can't decide which file has to be split and renamed.
Does anyone have an idea how to deal with it?
EDIT:
Ok I'll try to be a bit less vague.
The application I created creates very large xml-files. The name of this files are in the following format: FI.DB2P.107601.20130130.20010.T.1.1 . This name contains some identification numbers and the date when the file is created. The first part of the name is created in the Java application like this: FI.DB2P.107601.20130130.20010.T.
Now this file should be split into several chunks of max. 200 MB each. Then the created chunks should have the same name as the 'base-file' but they have to end with 'T.3.1', 'T.3.2' and 'T.3.3' for example.
My question now is how I can obtain the filename of the file created by the java application in the Unix script. The filename is pretty complex and contains data from the database so I can't define the name in the Unix script.
I hope it's a bit clearer now.
Is it not the case that your Java process will call the Unix script and therefore will be able to pass it the filename on the command line.
The Unix script can take the filename and run something like split on it and then fix-up the filenames to those that your Java process is expecting.
Unless I misunderstand your question that should be fairly easy to do.
When you create your XMLStreamWriter you know (hopefully) the name of the file:
String fileName = "FI.DB2P.107601.20130130.20010.T.1.1";
XMLStreamWriter writer = factory.createXMLStreamWriter(new FileWriter(fileName ));
Then it's not a problem to pass this name as a parameter to your shell script:
Runtime.getRuntime().exec("yourscript.sh " + fileName);
yourscript.sh will have code to split the file and add incrementing variable to the file name, something like this might work:
#!/bin/bash
split -b 200m -a 5 "$1" splited_file
i=1
for file in splited_file*
do
mv $file $1_${i}
i=$(( i + 1 ))
done
ps: this script is not thread safe :)
I am looking for a help regarding a shell script to redirect the output of a command to a file. I have a C program that reads the input from a serial port and display. I want this data to be redirected to a file. I am executing this from a java program by calling
Runtime r = Runtime.getRuntime();
Process procObj = r.exec("sh " + scriptfile);
I have tried writing the script file as
./program >> file.txt
The file.txt is not getting updated. Here, the program doesn't end until the connection to the port is lost, in a sense it is infinitely running. So my program keeps looking for data on the port and display as and when it is there.
I just need to redirect the same output to a file that I would use as a log.
I looked at How to make shell output redirect (>) write while script is still running? but not helpful.
Kindly help..
How much output does program generate? Using standard IO redirection will add a 4KB buffer between stdout and file. This means your program must output more than 4KB of data before the OS starts to write to the file.
To fix this, add stdout.flush() to your program when a "work unit" is complete (maybe a line but might be more than one line).
Can you try ./program >> file.txt 2>>file.txt, or ./program 2>&1 >>file.txt?
just try this
List<String> cmd = new ArrayList<String>();
cmd.add("sh");
cmd.add("-c");
cmd.add("program 1> file.txt 2>&1");
ProcessBuilder pb = new ProcessBuilder(cmd);
Process p = pb.start();
If you use standard C calls for output (printf, puts etc.), your output may get buffered. On C89 and onwards, it depends on the buffering mode (unbuffered, fully buffered, line buffered) and on the size of the buffer, whether your output is buffered at all and when the buffer is flushed (see http://www.gnu.org/software/libc/manual/html_node/Buffering-Concepts.html and man setvbuf).
By default, output to a file is fully buffered on Linux. If you want the output to appear immediately in the output file, you may:
use fflush() after each output operation
use the system call write() (man 2 write)
switch off buffering: setvbuf(stdout, NULL, _IONBF, 0); (https://stackoverflow.com/a/7876756/601203)
This behaviour is not related on the fact the you start your C program in a Java program via a shell script. This behaviour depends on the standard C library that you have linked into your program.
As I understand it, PHP's passthru displays the unadulterated output of the console - including all outputs, like STDERR. I'm trying to javac a file from a PHP file like so:
<?php
error_reporting(E_ALL);
if(file_put_contents("code.java", "aaaaaa"))
{
passthru("javac -verbose code.java");
echo("Done.");
}
else
{
echo("UNEXPECTED PHP ERROR");
}
?>
As you can probably guess, "aaaaaaa" should NOT compile - and this I expect output from the javac call (not to mention the -verbose). However, when I access the web page, I notice two things:
code.java is created successfully and filled with the data I specify.
The only output to the webpage is "Done."
Note that if I call the exact same command from the cmd prompt, I get a whole slew of output. What's going on here?
tl;dr; why am I not getting any output from passthru()?
EDIT: If I change the passthru command string to "echo PLEASEWORK" it displays outputs correctly
For some reason I can't comment -
Quamis, what IS the prefered method of executing this cmd and capturing all output?
In the man file http://php.net/manual/en/function.passthru.php i can read clearly "This function should be used in place of exec() or system() when the output from the Unix command is binary data which needs to be passed directly back to the browser". This means passthry is not your best friend for this task:)
This is not your case...
try using passthru("your_command_here 2>&1") to capture both stdout and stderr.
As mentioned, output to STDERR goes to Apache's log files. To "see" it in the browser, you'll need to redirect it to STDOUT:
$command = "javac -verbose code.java 2>&1";
$result = passthru($command);
This is called file descriptor redirection.
i have written a code to run .dat file using Java. but when is run that application then it take time to execute means it give half result and then after some time after gives complete result.
here is my code:
String file = config.getOutPath() + "run_doxygen.bat";
BufferedWriter out = new BufferedWriter(
new FileWriter(file));
String cmd = "doxygen " + config.getOutPath() + "Doxyfile";
runtime.exec(cmd);
System.out.println("cmd_doxy:"+cmd);
out.write(cmd);
out.newLine();
out.close();
the doxygen generate xml file. let suppose it generate 10 xml file . when i launch that *.bat file it generate 5 file and to generate rest 5 file it take time.
and *.bat file contain : doxygen "path"
path is location of config file. it work fine when i run it with cmd or double click.
anybody have any idea
.
thanks
May be you should flush the writer.
out.newLine();
out.flush();
out.close();
It is not entirely clear what you intend your program to do, but what it is actually is doing is as follows:
It opens the ".bat" file for writing.
It launches a "doxygen" command as a separate external process.
It writes the command to standard output, and then the file.
If you are saying that it takes some time for the output to be written to the file, well that is not entirely unexpected. The operating system may decide to give the newly launched doxygen application a big chunk of CPU time to get started. If it doesn't block, your Java application may not get a time-slice for a few seconds. And after that, the OS may switch between the two applications until one or the other finishes.
But why does it matter? Does your Java application expect / require doxygen to finish before it does?
If so, then the solution is to do something like this:
Process proc = runtime.exec(cmd);
// do more stuff.
int rc = proc.waitFor();
// Whoopee! the process has finished (or died) check the rc to see which.
if you are not consuming the streams generated by the external command, it can cause the program to hang. See this article which pretty much covers all the gotchas of using Runtime.exec.