g++: File not found - java

I have been developing a program lately that compiles and runs a C++ Program from a Java program, I have gotten everything working basically (or atleast to my knowledge) but then I noticed some things being printed to the Error Stream:
cdog5000#srv3:~$ java -Xmx50m -jar main2.jar
Running Command: sudo g++ --static -o "/home/cdog5000/cody.out" "/home/cdog5000/cody.cpp"
Err: g++: "/home/cdog5000/cody.cpp": No such file or directory
Err: g++: no input files
cdog5000#srv3:~$ ls -l
total 4548
-rwxr-xr-x 1 cdog5000 cdog5000 1297588 Feb 3 23:11 a.out
-rwxr-xr-x 1 cdog5000 cdog5000 7978 Feb 2 04:39 cody
-rw-r--r-- 1 cdog5000 cdog5000 106 Feb 4 02:09 cody.cpp
-rwxr-xr-x 1 cdog5000 cdog5000 1297357 Feb 4 02:09 cody.out
-rw-r--r-- 1 root root 410433 Feb 4 02:48 log.txt
-rwxr-xr-x 1 cdog5000 cdog5000 801088 Feb 1 05:24 main.jar
-rw-r--r-- 1 cdog5000 cdog5000 804802 Feb 4 02:49 main2.jar
drwxr-xr-x 3 cdog5000 cdog5000 4096 Feb 3 23:11 sandbox
cdog5000#srv3:~$ sudo g++ --static -o "/home/cdog5000/cody.out" "/home/cdog5000/cody.cpp"
As you can see it works if I do it via the SSH but not the Java code?
The Java code:
public static Exec exec(String cmd){
Exec exec = new Exec(cmd);
try {
long current = System.currentTimeMillis();
Process proc = Runtime.getRuntime().exec(cmd);
exec.setReturnValue(proc.waitFor());
exec.setRunTime(System.currentTimeMillis() - current);
BufferedInputStream bos = new BufferedInputStream(proc.getInputStream());
byte b[] = new byte[1024];
String content = "";
while(bos.read(b) != -1) {
content += new String(b);
}
exec.setStdIn(content.split("\n"));
content = "";
bos = new BufferedInputStream(proc.getErrorStream());
while(bos.read(b) != -1) {
content += new String(b);
}
exec.setStdErr(content.split("\n"));
} catch (Exception e) {
e.printStackTrace();
}
return exec;
}
Thanks for any help and it is apprectiated!

Err: g++: "/home/cdog5000/cody.cpp": No such file or directory
Is telling you the problem.
You have one level of quotes too many, so you're looking for "/home/cdog5000/cody.cpp" rather than /home/cdog5000/cody.cpp.
The Runtime.exec documentation says:
More precisely, the command string is broken into tokens using a StringTokenizer created by the call new StringTokenizer(command) with no further modification of the character categories. The tokens produced by the tokenizer are then placed in the new string array cmdarray, in the same order.
Meaning it only splits on whitespace, it doesn't handle double quotes like the shell does.
Many languages have two functions, one called exec which runs the command verbatim, and system which passes the string to the shell, where it will split words and expand wildcards.
I can't see a system call in Java, so I think you will have to use exec(String[] cmdarray) rather than exec(String command).

GCC doesn't lie like that - it looks like the file isn't there. Are you sure that you're showing us the output from the correct directories?

Related

Perl system call to jar file failing in cron

I am trying to execute a perl script via a cron job that uses the system command to execute a java wrapper for boilerpipe. It fails when executed via cron. The script (simplified) is
my $link = "http://foo.bar"; # with a real link
my $cmdstring = 'java -jar boilerpipe-wrapper.jar url '.$link.' out.txt';
my $result = system($cmdstring);
if ($? == -1) {
print LOG "failed to execute system command: $!\n";
die;
}
elsif ($? & 127) {
printf LOG "child died with signal %d, %s coredump\n",
($? & 127), ($? & 128) ? 'with' : 'without';
die;
}
else {
printf LOG "child exited with value %d\n", $? >> 8;
die;
}
The die commands are in there for debugging purposes. What appears in LOG is
child exited with value 1
The crontab line is
24 7 * * * perl /home/retrievetext.pl >> /home/cron-msgs 2>&1
What appears in cron-msgs is
Error: unable to access jarfile boilerpipe-wrapper.jar
Died at /home/retrievetext.pl line 15
The ls -la for boilerpipe-wrapper.jar is
-rwxr-xr-x 1 steve sudo 1945275 Apr 29 03:53 boilerpipe-wrapper.jar
The Perl script and the system call work as expected when executed from the terminal. Anyone know what the problem is?

WEKA quote parse error

While using java "setOptions" function with the bellow properties I am getting an error : "Quote parse error."...
"weka.classifiers.meta.OneClassClassifier -num \"weka.classifiers.meta.generators.GaussianGenerator -S 1 -M 0.0 -SD 1.0\" -nom \"weka.classifiers.meta.generators.NominalGenerator -S 1\" -trr 0.1 -tcl F&B -cvr 10 -cvf 10.0 -P 0.5 -S 1 -W weka.classifiers.meta.Bagging -- -P 100 -S 1 -num-slots 1 -I 10 -W weka.classifiers.trees.REPTree -- -M 2 -V 0.001 -N 3 -S 1 -L -1 -I 0.0"
I understand that this is occurring because of error in applying quotes, but I am not sure as to where they should be applied.
I thought of applying them before calling REPTree but that doesn't seem to work.
Whenever using a Weka classifier with Java code, use the weka.core.Utils.splitOptions method to parse the config string without worrying about quoting and escaping characters.
// set your configurations parameters here, e.g., "-S 1"
String options = "whatever configuration you want";
// instantiate the classifier object
REPTree tree = new REPTree();
// parse and set the classifier's configuration
tree.setOptions(Utils.splitOptions(options));
Also, remember to provide your current code and to format your question.

write logs to oldest file in directory

I want to do some tweaks to my logging for my application...
I would like some help to enhance what I have below in main method:
public static void main(String[] args) {
try {
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Handler h = new FileHandler("../logs/MyLogFile_"
+ sdf.format(date) + ".log", true);
h.setFormatter(new SingleLineFormatter());
h.setLevel(Level.ALL);
logger.setUseParentHandlers(false);
logger.addHandler(h);
}
//...
}
It creates a log file with date stamp everytime I run the application. But I want to achieve something like this in my Unix Directory:
-rw-r--r-- 1 r787848 dev 45271 Feb 4 11:31 MyLogFile.log.06
-rw-r--r-- 1 r787848 dev 45308 Feb 5 11:36 MyLogFile.log.05
-rw-r--r-- 1 r787848 dev 44336 Feb 6 06:50 MyLogFile.log.04
-rw-r--r-- 1 r787848 dev 44379 Feb 7 08:41 MyLogFile.log.03
-rw-r--r-- 1 r787848 dev 44409 Feb 10 08:45 MyLogFile.log.02
-rw-r--r-- 1 r787848 dev 44446 Feb 11 12:36 MyLogFile.log.01
I want to define a set of lets say 6 log files to capture logging of daily run of the application. When it comes to logging, I want the application to write to the log file that is oldest, so in the above instance, running the application on Feb 12 08:45 should clear MyLogFile.log.06 and write fresh for feb 12.
How can this be achieved with java.util.logging on top of what I have. Unfortunately, I am not able to configure log4j properties and want to use java.util.logging only.
The only close approximation is to do the following:
Handler h = new FileHandler("../logs/MyLogFile_"
+ sdf.format(date) + ".log", Integer.MAX_VALUE, 6, false);
See: JDK-6350749 - Enhance FileHandler to have Daily Log Rotation capabilities.

Why FileNotFoundException is thrown while it exists on linux

This is the first time i have encounter such problem with file access by Java on linux. The problem is just like the header says - FileNotFoundException is thrown when file actually exists. Moreover application with same configuration (props.txt file) runs like it should on windows.
Let me provide a little bit of console output
datasu#dedi2392:~/netcrawler/dkpto$ ls -l
total 20
-rwxrw-rw- 1 datasu datasu 114 Aug 7 15:53 autoupdate
drwxr-xr-x 4 datasu datasu 4096 Aug 8 11:57 data
drwxr-xr-x 2 datasu datasu 4096 Aug 8 11:57 log
-rw-rw-rw- 1 datasu datasu 32 Aug 8 12:44 props.txt
-rwxrw-rw- 1 datasu datasu 126 Aug 8 12:55 propsUpdate
datasu#dedi2392:~/netcrawler/dkpto$ ./propsUpdate
Parent: /usr/home/datasu/netcrawler/dkpto
1# -> propsUpdate
2# -> autoupdate
3# -> props.txt
4# -> data
5# -> log
(No such file or directory)ava.io.FileNotFoundException: /usr/home/datasu/netcrawler/dkpto/props.txt
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.(Unknown Source)
at netcrawler.Autoupdater.readProperties(Autoupdater.java:71)
at netcrawler.Autoupdater.start(Autoupdater.java:54)
at netcrawler.Autoupdater.main(Autoupdater.java:47)
datasu#dedi2392:~/netcrawler/dkpto$ java -version
java version "1.6.0_45"
Java(TM) SE Runtime Environment (build 1.6.0_45-b06)
Java HotSpot(TM) 64-Bit Server VM (build 20.45-b01, mixed mode)
datasu#dedi2392:~/netcrawler/dkpto$
and here is Java code responsible for generating that output (at least after calling ./propsUpdate)
private void readProperties(String args) throws FileNotFoundException, IOException {
System.out.println("Parent: " + new File(args).getAbsoluteFile().getParentFile().getAbsolutePath());
CommonTools.PrintArray(new File(args).getAbsoluteFile().getParentFile().list());
properties.load(new FileInputStream(new File(args).getAbsoluteFile())); // this line throws the exception
stageNumber = Integer.parseInt(properties.getProperty(PROP_STAGE_NUMBER_KEY, "0"));
}
So why the props.txt file is not found when it is actually there ?
The string "args" probably has a nonprinting character at the end, like a space. You could use String.trim() to remove such characters before using that variable.
Is your home folder really this path?
/usr/home/datasu
/home/datasu is where it normally is on linux.
Also, try changing that line to this:
properties.load(new FileInputStream(new File(args));
If you're calling that as ./propsUpdate ./props.txt that will work from the current working directory.

ProcessBuilder gives a "No such file or directory" on Mac while Runtime().exec() works fine

I have an application, running on the Playframework, which needs to encode some video files. I used
Process pr = Runtime.getRuntime().exec(execCode)
for this (and it works perfectly), but as I need both, the output stream and the error stream, I am trying to use ProcessBuilder (as is also recommended).
But I cannot get it to work (testing on a MacBook). Is there a fundamental difference between the Runtime method and the ProcessBuilder?
This is my code for ProcessBuilder (exactly the same code works when replaced by Runtime.getRuntime().exec())
String execCode = "/opt/local/bin/ffmpeg -i file [...]";
ProcessBuilder pb = new ProcessBuilder(execCode);
pb.redirectErrorStream(true);
pb.directory(new File("/Users/[...]/data/"));
Process pr = pb.start();
This is the console output:
11:00:18,277 ERROR ~ There was a problem with with processing MediaFile[13] with error Error during coding process: Cannot run program "/opt/local/bin/ffmpeg -i /Users/[...]/data/media/1/1/test.mov [...] /Users/[...]/data/media/1/13/encoded.mp3" (in directory "/Users/[...]/data"): error=2, No such file or directory
java.lang.Exception: Error during coding process: Cannot run program "/opt/local/bin/ffmpeg -i /Users/Luuk/Documents/Java/idoms-server/data/media/1/1/test.mov -y -f mpegts -acodec libmp3lame -ar 48000 -b:a 64000 -vn -flags +loop -cmp +chroma -partitions +parti4x4+partp8x8+partb8x8 -subq 5 -trellis 1 -refs 1 -coder 0 -me_range 16 -keyint_min 25 -sc_threshold 40 -i_qfactor 0.71 -bt 200k -maxrate -1 -bufsize -1 -rc_eq 'blurCplx^(1-qComp)' -qcomp 0.6 -qmin 10 -qmax 51 -qdiff 4 -level 30 -g 30 -async 2 /Users/Luuk/Documents/Java/idoms-server/data/media/1/13/encoded.mp3" (in directory "/Users/Luuk/Documents/Java/idoms-server/data"): error=2, No such file or directory
at logic.server.MediaCoder.encodeMediaFile(MediaCoder.java:313)
at logic.server.MediaCoder.doJob(MediaCoder.java:54)
at play.jobs.Job.doJobWithResult(Job.java:50)
at play.jobs.Job.call(Job.java:146)
at play.jobs.Job$1.call(Job.java:66)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
at java.util.concurrent.FutureTask.run(FutureTask.java:138)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$301(ScheduledThreadPoolExecutor.java:98)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:206)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
at java.lang.Thread.run(Thread.java:680)
Caused by: java.io.IOException: Cannot run program "/opt/local/bin/ffmpeg -i /Users/Luuk/Documents/Java/idoms-server/data/media/1/1/test.mov -y -f mpegts -acodec libmp3lame -ar 48000 -b:a 64000 -vn -flags +loop -cmp +chroma -partitions +parti4x4+partp8x8+partb8x8 -subq 5 -trellis 1 -refs 1 -coder 0 -me_range 16 -keyint_min 25 -sc_threshold 40 -i_qfactor 0.71 -bt 200k -maxrate -1 -bufsize -1 -rc_eq 'blurCplx^(1-qComp)' -qcomp 0.6 -qmin 10 -qmax 51 -qdiff 4 -level 30 -g 30 -async 2 /Users/Luuk/Documents/Java/idoms-server/data/media/1/13/encoded.mp3" (in directory "/Users/Luuk/Documents/Java/idoms-server/data"): error=2, No such file or directory
at java.lang.ProcessBuilder.start(ProcessBuilder.java:460)
at logic.server.MediaCoder.encodeMediaFile(MediaCoder.java:189)
... 11 more
Caused by: java.io.IOException: error=2, No such file or directory
at java.lang.UNIXProcess.forkAndExec(Native Method)
at java.lang.UNIXProcess.<init>(UNIXProcess.java:53)
at java.lang.ProcessImpl.start(ProcessImpl.java:91)
at java.lang.ProcessBuilder.start(ProcessBuilder.java:453)
... 12 more
You need to specify the arguments as separate Strings:
new ProcessBuilder("cmd", "arg1", "arg2", ...);
The constructor accepts String, varargs, and List<String>.
See ProcessBuilder documentation.
If you don't want to break your commands into tokens everytime, you could try
new ProcessBuilder("sh", "-c", execCode); // Linux / Unix terminal
OR
new ProcessBuilder("cmd", "/c", execCode); // Windows command line

Categories

Resources