Where is the R command mac - java

I know this has kind of been asked before but I just don't know how to locate the program so I can run the R script.
I have a java swing program and on it I have a button whose action should be to run an external R script. I keep getting a "No such file or directory error" and I'm sure its because I don't know what command I should be running. I have RStudio installed. My code:
JButton btnSelfassessR = new JButton("SelfAssess R");
btnSelfassessR.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
Runtime.getRuntime().exec(new String[] { "/usr/bin/Rscript","Rscript /Users/sebastianzeki/EndoHistol.R"});
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
});
but actually I think the /usr/bin/Rscript is wrong and I don't know how to correct this. What should I be looking to put in here. What is the likely location for a standard install of RStudio on a mac El Capitan

Related

Runtime.exec() issues with java when attempting to execute scripts, but certain commands work

I am attempting to have my java program execute a python script using
import java.lang.Runtime;
public class test
{
public static void main(String[] args)
{
try
{
System.out.println("testing");
Runtime.getRuntime().exec("/usr/bin/python print.py");
}catch(Exception e){System.out.println("not working");}
}
}
However, nothing is shown on the terminal, (print.py simple prints "THIS IS WORKING"). In contrast to this, when I use
Runtime.getRuntime().exec("touch dog.txt");
A file named dog.txt is created.
I also attempted to run
Runtime.getRuntime()exec("./shellscript.sh");
Which is just a script that runs the touch command, that also did not work.
Not really sure what the issue is here, and even more interesting is that yesterday the java program was working as intended with no large changes to my computer in the time in between. Anybody have any ideas on whats going on?
I am not receiving any errors.
I guess you have to wait to end of script execution.
try {
Process process = Runtime.getRuntime().exec("/usr/bin/python print.py");
process.waitFor();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
You can try to read exitCode from process ;-)
Ok I ended up having success by using
process p = Runtime.getRuntime().exec("./pyshellthing.sh");
p.waitFor();
The shell script simply executes the python script.

JMeter does not execute Java code correctly if it's ran as .jar file from command line

I'm designing JMeter scenario which implies executing a certain .jar file via OS Process Sampler element. My Java code has while loop which basically checks a certain mailbox for a letter with a certain subject. Loop waits until finds one (emails are always delivered with roughly 3 minutes delay), parses it and writes some data to .txt file.
If I run this .jar directly from cmd then the code works as expected. But if I run it via JMeter OS Process Sampler then it never creates a file for me. I do see that email is delivered to inbox, so expect it to be parsed and .txt created.
At first I suspected that JMeter finishes Java scenario without waiting for while loop to execute. Then I put OS Process Sampler in a separate Thread and added a huge delay for this thread in order to wait and make 100% sure that email is delivered and Java only need to parse it but it does not help.
View Results Tree never shows any errors.
Here is my OS Process Sampler: https://www.screencast.com/t/LomYGShJHAkS
This is what I execute via cmd and it works as expected: java -jar mailosaurJavaRun.jar email533.druzey1a#mailosaur.in
And here is my code (it does not looks good but it works):
public class Run {
public static void main(String[] args) {
MailosaurHelper ms = new MailosaurHelper();
String arg1 = ms.getFirstLinkInEmail(args[0]);
BufferedWriter output = null;
try {
File file = new File("url.txt");
output = new BufferedWriter(new FileWriter(file));
output.write(arg1);
} catch ( IOException e ) {
e.printStackTrace();
} finally {
if ( output != null ) {
try {
output.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
public class MailosaurHelper {
protected final String API_KEY = "b3e4d2b193b5eb2";
protected final String MAILBOX_ID = "d1uzey1a";
public MailboxApi getEmailBox() {
return new MailboxApi(MAILBOX_ID, API_KEY);
}
public String getFirstLinkInEmail(String email) {
MailosaurHelper ms = new MailosaurHelper();
String link = "";
if (link.equals("") || link.isEmpty()) {
try {
Thread.sleep(3000);
link = ms.getAllEmailsByReceipent(email)[0].html.links[0]
.toString();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return link;
}
public Email[] getAllEmailsByReceipent(String recepient) {
try {
int ifArrayIsEmpty = getEmailBox().getEmailsByRecipient(recepient).length;
while (ifArrayIsEmpty == 0) {
try {
Thread.sleep(3000);
ifArrayIsEmpty = getEmailBox().getEmailsByRecipient(
recepient).length;
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
} catch (MailosaurException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
Email[] listOfEmails = null;
try {
listOfEmails = getEmailBox().getEmailsByRecipient(recepient);
} catch (MailosaurException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return listOfEmails;
}
The bottom line is that I need to parse Mailosaur email, retrieve URL from it and use it further. Any other suggestion on how to do that using Jmeter/Java/Mailosaur are appreciated.
You don't need cmd in here, but if you're adamant to stick with it - use /C key when you call it.
Then, are your sure you're looking for your file in the right place?
According to documentation:
By default the classes in the java.io package always resolve relative
pathnames against the current user directory. This directory is named
by the system property user.dir, and is typically the directory in
which the Java virtual machine was invoked.
Check it thoroughly, BTW - you should see it in your sampler result.

The command works on terminal but doesn't work in java program

I wrote a process manager program one of the things that it does is to exit all running processes when it shut down
,so there is the code
public void stop_all() throws IOException {
Process p = Runtime.getRuntime().exec("kill -9 -1");
System.out.println("killed");
}
and there is the action on the button
private void exitButton(java.awt.event.ActionEvent evt) {
Run ob = new Run();
try {
ob.stop_all();
} catch (IOException ex) {
Logger.getLogger(mainmenu.class.getName()).log(Level.SEVERE, null, ex);
}
this.dispose();
}
i have no idea why it doesn't work ,
i execute that command in the terminal and it works fine
please help :)
I am still skeptical about the permissions of the program. But, from this reference you need to specify the command path in your exec().
So your code should probably be:
public void stop_all() throws IOException {
Process p = Runtime.getRuntime().exec("/bin/kill -9 -1");
System.out.println("killed");
}

Create a PDF file using LaTex templates and Java

I need to create a report system using LaTex templates and Java like programming language. I use JLR library but it's freeeware, this is my code:
File workingDirectory = new File("./config/output");
File desktop = new File("./config/desktop");
File invoice1 = new File("./config/templates/template1.tex");
File invoice2 = new File("./config/templates/template2.tex");
JLRGenerator pdfGen = new JLRGenerator();
pdfGen.deleteTempTexFile(false);
if (!pdfGen.generate(invoice1, desktop, workingDirectory)) {
System.out.println(pdfGen.getErrorMessage());
}
JLROpener.open(pdfGen.getPDF());
if (!pdfGen.generate(invoice2, desktop, workingDirectory)) {
System.out.println(pdfGen.getErrorMessage());
}
JLROpener.open(pdfGen.getPDF());
Searching into the Web I find JLatexMath but, as far as I know, only generates equations in LaTex and not the entire PDF. Do you know a library in Java which generates an entire PDF file using a LaTex template?
Update: I execute de .tex file using Runtime.getRuntime().exec("pdflatex.exe...") command. But I don't achive save the PDF file.
Thanks in advance
I think I have the solution, here is the code:
public void generateReport()
{
Process p;
try {
p = Runtime.getRuntime().exec("C:\\pdflatex.exe -synctex=1 -interaction=nonstopmode ./config/log/document.tex");
p.waitFor();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
It generates a .PDF and .dvi file in the same place of your .tex file. Thank you much for your help :)

How put string to Shared Preference?

Could anybody help me, please? I have this code:
Process a;
try {
a = Runtime.getRuntime().exec("su");
DataOutputStream swp = new DataOutputStream(a.getOutputStream());
swp.writeBytes("cat /proc/sys/vm/blabla\n");
swp.writeBytes("exit\n");
swp.flush();
try {
a.waitFor();
if (a.exitValue() != 255) {
// TODO Code to run on success
toastMessage("root");
}
else {
// TODO Code to run on unsuccessful
toastMessage("not root");
}
} catch (InterruptedException e) {
// TODO Code to run in interrupted exception
toastMessage("not root");
}
} catch (IOException e) {
// TODO Code to run in input/output exception
toastMessage("not root");
}
This code swp.writeBytes("cat /proc/sys/vm/blabla\n"); copies text (string) from system file called "blabla". And then I need to put this text (String) to my SharedPreferences. How can I achieve it?
If this was your code, you should know. In case you don't, you can follow the Tutorial.
In your case, you'll want to use the putString()-method.
To get what cat printed out, you'll need the output of your Process-object. Use the getInputStream()-method.

Categories