7
$\begingroup$

Is it possible to use J/Link in a Wolfram Programming Cloud notebook? I need it to load and use 3rd-party .jars.

$\endgroup$
  • 2
    $\begingroup$ initial testing revealed that OnlineM lets you load JLink with Needs["JLink`"], but then upon ReinstallJava[] the kernel quits. I've also seen that it uses Java 7 update 13, which is quite depressing, given that the current Java is Java 8 update 11 since Jul 15. I'd assume that CloudM uses the same as OnlineM, but haven't been able to test yet. $\endgroup$ – Andreas Lauschke Jul 25 '14 at 0:00
5
$\begingroup$

You can load Java in the cloud, in either the interactive web interface or in deployed code. (Some things are different about those environments.)

CloudEvaluate[
  Needs@"JLink`";
  str = JavaNew["java.lang.String", "Hello, world!"];
  JavaObjectToExpression[str]]

"Hello, world!"

As pointed out by Andreas Lauschke, ReinstallJava[] crashed the kernel in the programming cloud interface for me as well, but interestingly it does not crash here:

CloudEvaluate[
  Needs@"JLink`";
  ReinstallJava[];
  str=JavaNew["java.lang.String","Hello, world!"];
  JavaObjectToExpression[str]]

But it does not appear you can load jars, or edit the Java classpath.

jar = BinaryReadList@"HelloWorld.jar";
With[{j = jar},
 CloudEvaluate[
  path = "jars/HelloWorld.jar";
  CreateDirectory["jars"];
  BinaryWrite[path, j];
  Close[path];
  Needs@"JLink`";
  AddToClassPath[ClassPath -> path];
  {FileExistsQ@path, FreeQ[JavaClassPath[], path]}
  (*LoadJavaClass["HelloWorld"];
  Test`hello[]*)]]

{True, True}

I guess this is for security reasons.

$\endgroup$
  • 1
    $\begingroup$ thanks for your answer. One clarifying question: how would you go about loading custom .jars in that environment? Is there a way to upload them to Wolfram Cloud? Or at least to access them from a remote location? $\endgroup$ – Alex Born Jul 25 '14 at 16:05
  • $\begingroup$ You can upload files to the WolframCloud either in the cloud (use up arrow next to the red "New" button) or use CopyFile[-local file-,CloudObject["cloud file name"]]. Also, in a Cloud notebook, you can load jar files present in the cloud. You can't use CloudEvaluate or deployed APIFunctions to run Java code, as mfvonh has shown. $\endgroup$ – ChadK Jul 25 '14 at 20:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.