So I want to make a java application in eclipse which the user i will be able to import .zip files. Each .zip file will represent a cat breed. I will click on a "train" button and my program will contact IBM Watson services and create a classifier. Then from a different window, i will import random cat images and the program will show what cat breed is in the image. Everything with the SDKs is fine since I ran some examples from the official Watson site and everything ran smoothly. Problem comes when I try to create my own classifiers. The code you are about to see is also from their site. For some reason the createClassifier method won't take the CreateClassifierOptions object as an argument.
import java.io.File;
import com.ibm.watson.developer_cloud.http.ServiceCall;
import com.ibm.watson.developer_cloud.speech_to_text.v1.model.RecognitionCallback;
import com.ibm.watson.developer_cloud.visual_recognition.v3.*;
import com.ibm.watson.developer_cloud.visual_recognition.v3.model.*;
public class TrainningClassifier{
public static void main(String[] args) {
VisualRecognition service = new VisualRecognition(
VisualRecognition.VERSION_DATE_2016_05_20
);
service.setApiKey("aca4433597018de62edafdeebceb2bdc1482496a");
CreateClassifierOptions createClassifierOptions = new CreateClassifierOptions.Builder()
.name("dogs")
.addClass("beagle", new File("./beagle.zip"))
.addClass("goldenretriever",new File("./golden-retriever.zip"))
.addClass("husky", new File("./husky.zip"))
.negativeExamples(new File("./cats.zip"))
.build();
Classifier dogs = service.createClassifier(createClassifierOptions).execute();
System.out.println(dogs); /*error is in the above line.
the createClassifier method.*/
}
}
Error: Exception in thread "main" java.lang.Error: Unresolved
compilation problem: The method createClassifier(ClassifierOptions)
in the type VisualRecognition is not applicable for the arguments
(CreateClassifierOptions)
at testVisualRec.ForAssignment.main(ForAssignment.java:31)
Any ideas?
Found the solution. For some reason eclipse wouldn't recommend this solution I had to experiment. I just added throws IOException in main method. I also put inside the main method System.out.println(new File(".").getAbsoluteFile()); to make sure the path was correct, and it was. (SDK used for this project is 4.0.0, not the newest one. SDK found here: https://github.com/watson-developer-cloud/java-sdk/releases)
Related
I couldn't find a solution create a polyglot source out of multiple files in GraalVM.
What exactly I want to achieve:
I have a python project:
my-project:
.venv/
...libs
__main__.py
src/
__init__.py
Service.py
Example sourcecode:
# __main__.py
from src.Service import Service
lambda url: Service(url)
# src/Service.py
import requests
class Service:
def __init__(self, url):
self.url = url
def invoke(self):
return requests.get(self.url)
This is very simple example, where we've got an entry-point script, project is structured in packages and there is one external library (requests).
It works, when I run it from command-line with python3 __main__.py, but I can't get it work, when embedding it in Java (it can't resolve imports).
Example usage in java:
import org.graalvm.polyglot.Context;
import org.graalvm.polyglot.Source;
import org.graalvm.polyglot.Value;
import java.io.File;
import java.io.IOException;
public class Runner {
public static void main(String[] args) throws IOException {
Context context = Context.newBuilder("python")
.allowExperimentalOptions(true)
.allowAllAccess(true)
.allowIO(true)
.build();
try (context) {
// load lambda reference:
Value reference = context.eval(Source.newBuilder("python", new File("/path/to/my-project/__main__.py")).build());
// invoke lambda with `url` argument (returns `Service` object)
Value service = reference.execute("http://google.com");
// invoke `invoke` method of `Service` object and print response
System.out.println("Response: " + service.getMember("invoke").execute());
}
}
}
It fails with Exception in thread "main" ModuleNotFoundError: No module named 'src'.
The solution works for javascript project (having similar index.js to __main__.py, its able to resolve imports - GraalVM "sees" other project's files, but somehow it doesn't, when using python.
I found out, that python is able to run zip package with project inside, but this also doesn't work with GraalVM.
Is there any chance to accomplish it? If not, maybe there is a similar tool to webpack for python (if I could create a single-file bundle, it should also work).
Btw, I don't know python at all, so I may missing something.
Thanks for any help!
At school, I write Java programs with Windows’ Java Editor (console mode). There, InOut.readInt() (used for user input) works without problems and I don’t have to import anything.
Now, I have a Java homework for the holidays, and I try to write Java programs on my Mac. In online console Java editors, the line InOut.readInt() causes this error:
/IntBetween10And100.java:8: error: cannot find symbol
int input = InOut.readInt("Integer --> ");
^
symbol: variable InOut
location: class IntBetween10And100
1 error
I already tried import lines (placed before the class) like:
import java.*
import java.util.*
import java.util.InOut
import java.io.BufferedStreamReader
import java.util.*;
public class IntBetween10And100 {
public static void main(String args[]) {
int input = InOut.readInt("Integer --> ");
}
}
int input = InOut.readInt("Integer --> ");
should produce the line
Integer -->
but instead, the error message (seen above) appears.
OK, so you are using the "Java-Editor" tool on Windows for writing your Java code.
It turns out that Java-Editor includes a class called InOut as an example class. (You can see it here: http://javaeditor.org/doku.php?id=en:examples).
For various reasons, it is not suitable for use in production code of any kind:
It is not part of the Java SE class library, or any 3rd-party libraries.
It is a class in the default package
It has limited functionality, even compared to the real System.in and System.out
It would interfere with any application or 3rd party library code that uses System.in in the normal way. (It creates its own BufferedReader to wrap System.in. That is liable to capture "type-ahead" input.)
You don't really need to use it for educational purposes either. It is only a wrapper class ...
However, if you want to use InOut outside of the Java-Editor context, you could simply download the source code from the page above and add it to your project. I can't tell you exactly how, but adding classes should be explained in the documentation of the tool you are using now! (If you are serious about learning Java, I would actually recommend that you download and install a real Java JDK and a real Java IDE on your own computer.)
The authors have neglected to include an explicit copyright notice in the InOut.java file. However, the Java-Editor site as a whole is licensed as "CC Attribution-Share Alike 4.0 International".
I have trained a Decision Table ML model in Weka 3.8 Dekstop Version. I have saved the model accordingly in my assets folder and configured the wekaSTRIPPED.jar file as well. At this point, the build seems to be working fine on Android Studio. However, when I try to use the classification model in Java, I am getting some red highlights - errors - over my code which I am unable to autofix.
I have visited some online guides here and visited this commonly used weka-android reference here and here. They follow the same structure as what I have been doing in my code (with different models), but mine does not compile in Android Studio.
package com.example.owner.introductoryapplication;
import android.support.v7.app.AppCompatActivity;
import weka.classifiers.Classifier;
import weka.classifiers.rules.DecisionTable;
import weka.core.Attribute;
import weka.core.DenseInstance;
import weka.core.Instances;
import java.util.ArrayList;
public class Test extends AppCompatActivity {
public static void main(String[] args) throws Exception {
Test test = new Test();
test.start();
}
public void start() throws Exception {
//LOADS THE MODEL...
String rootPath = "/assets/";
String fileName = "PGBD_DecisionTableUPD.model";
//Classifier cls = null;
Classifier cls = (Classifier) weka.core.SerializationHelper.read(rootPath + fileName);
I expect the output to simply build; I don't expect any prediction output because I haven't specified the testing data-set. However, when I do click compile, none of the terms are highlighted red on the screen.
Specifically, immediately after I run it, I get Compilation failed; see the compiler error output for details. in the build console followed by:
"C:\Program Files\Android\Android Studio\jre\bin\java.exe" -Didea.launcher.port=64163 "-Didea.launcher.bin.path=C:\Program Files\Android\Android Studio\bin" -Dfile.encoding=UTF-8 -classpath "C:\Users\Owner\AppData\Local\Android\Sdk\platforms\android-28\android.jar;C:\Users\Owner\AppData\Local\Android\Sdk\platforms\android-28\data\res;C:\Users\Owner\AndroidStudioProjects\IntroductoryApplication\app\build\intermediates\javac\debug\compileDebugJavaWithJavac\classes;C:\Users\Owner\AndroidStudioProjects\IntroductoryApplication\app\build\generated\res\rs\debug;C:\Users\Owner\AndroidStudioProjects\IntroductoryApplication\app\build\generated\res\resValues\debug;C:\Users\Owner\.gradle\caches\transforms-1\files-1.1\customview-28.0.0.aar\14e09720fc7f657365dec6e786490d47\jars\classes.jar;C:\Users\Owner\.gradle\caches\transforms-1\files-1.1\localbroadcastmanager-28.0.0.aar\d791f241c61f09475b5d43561b131f77\jars\classes.jar;C:\Users\Owner\.gradle\caches\transforms-1\files-1.1\support-vector-drawable-28.0.0.aar\c8f91860fd6811b7040ad585c1bb749f\jars\classes.jar;C:\Users\Owner\.gradle\caches\transforms-1\files-1.1\interpolator-28.0.0.aar\73289dbc6711e8292415efe83a859a43\jars\classes.jar;C:\Users\Owner\.gradle\caches\transforms-1\files-1.1\support-core-utils-28.0.0.aar\2dc7d30dd7abd1c4b0846462e1137cc4\jars\classes.jar;C:\Users\Owner\.gradle\caches\transforms-1\files-1.1\support-core-ui-28.0.0.aar\b5199754cf7a7bf403b3073b69edcfe3\jars\classes.jar;C:\Users\Owner\AndroidStudioProjects\IntroductoryApplication\app\libs\wekaSTRIPPED.jar;C:\Users\Owner\.gradle\caches\transforms-1\files-1.1\slidingpanelayout-28.0.0.aar\32aa91f4149120a8a428b24c5291b432\jars\classes.jar;C:\Users\Owner\.gradle\caches\transforms-1\files-1.1\viewmodel-1.1.1.aar\3477f32913f9b9934a8185f031c05533\jars\classes.jar;C:\Users\Owner\.gradle\caches\transforms-1\files-1.1\drawerlayout-28.0.0.aar\bfb4eb97f205f1db9a43db5a200619a5\jars\classes.jar;C:\Users\Owner\.gradle\caches\transforms-1\files-1.1\coordinatorlayout-28.0.0.aar\aba273524bbeff5bfab162bc52fb07b9\jars\classes.jar;C:\Users\Owner\.gradle\caches\transforms-1\files-1.1\coordinatorlayout-28.0.0.aar\aba273524bbeff5bfab162bc52fb07b9\res;C:\Users\Owner\.gradle\caches\modules-2\files-2.1\com.android.support\collections\28.0.0\c1bcdade4d3cc2836130424a3f3e4182c666a745\collections-28.0.0.jar;C:\Users\Owner\.gradle\caches\transforms-1\files-1.1\documentfile-28.0.0.aar\e49f57095504cc48cc7ba2b26757daf1\jars\classes.jar;C:\Users\Owner\.gradle\caches\transforms-1\files-1.1\constraint-layout-1.1.3.aar\96728e03169a340c5e642682fbd18ff8\jars\classes.jar;C:\Users\Owner\.gradle\caches\transforms-1\files-1.1\constraint-layout-1.1.3.aar\96728e03169a340c5e642682fbd18ff8\res;C:\Users\Owner\.gradle\caches\transforms-1\files-1.1\swiperefreshlayout-28.0.0.aar\a560a21fcde6d062081d9c09634c92a8\jars\classes.jar;C:\Users\Owner\.gradle\caches\transforms-1\files-1.1\cursoradapter-28.0.0.aar\733ca7f2a61e46bdb2164e238fe8c4b6\jars\classes.jar;C:\Users\Owner\.gradle\caches\transforms-1\files-1.1\asynclayoutinflater-28.0.0.aar\f950d05bc8679714fb0d7642259e29eb\jars\classes.jar;C:\Users\Owner\.gradle\caches\transforms-1\files-1.1\livedata-1.1.1.aar\400a5420ad5e4bcef8ebbebf2123e101\jars\classes.jar;C:\Users\Owner\.gradle\caches\modules-2\files-2.1\android.arch.core\common\1.1.1\e55b70d1f5620db124b3e85a7f4bdc7bd48d9f95\common-1.1.1.jar;C:\Users\Owner\.gradle\caches\transforms-1\files-1.1\versionedparcelable-28.0.0.aar\12c3dd068050bf0422d8455749982d52\jars\classes.jar;C:\Users\Owner\.gradle\caches\transforms-1\files-1.1\runtime-1.1.1.aar\68e9f88e3e623f899ee79dd4aa8966f1\jars\classes.jar;C:\Users\Owner\.gradle\caches\transforms-1\files-1.1\print-28.0.0.aar\f12fdac753fb8cbf34b55eced23a29c7\jars\classes.jar;C:\Users\Owner\.gradle\caches\transforms-1\files-1.1\loader-28.0.0.aar\0f1fa1caaba4127fdcb1d7bb76f1c144\jars\classes.jar;C:\Users\Owner\.gradle\caches\transforms-1\files-1.1\viewpager-28.0.0.aar\bdf7976216feddc6d17d8052bf1807db\jars\classes.jar;C:\Users\Owner\.gradle\caches\transforms-1\files-1.1\support-fragment-28.0.0.aar\df9931f8c3d84779819dccebe1aa6282\jars\classes.jar;C:\Users\Owner\.gradle\caches\modules-2\files-2.1\com.android.support\support-annotations\28.0.0\ed73f5337a002d1fd24339d5fb08c2c9d9ca60d8\support-annotations-28.0.0.jar;C:\Users\Owner\.gradle\caches\modules-2\files-2.1\android.arch.lifecycle\common\1.1.1\207a6efae6a3555e326de41f76bdadd9a239cbce\common-1.1.1.jar;C:\Users\Owner\.gradle\caches\transforms-1\files-1.1\livedata-core-1.1.1.aar\f5b8b9e2bbbf6e16caa00d091d538f2f\jars\classes.jar;C:\Users\Owner\.gradle\caches\transforms-1\files-1.1\support-compat-28.0.0.aar\0fefdb1435b453663f8b867197d75a41\jars\classes.jar;C:\Users\Owner\.gradle\caches\transforms-1\files-1.1\support-compat-28.0.0.aar\0fefdb1435b453663f8b867197d75a41\res;C:\Users\Owner\.gradle\caches\transforms-1\files-1.1\animated-vector-drawable-28.0.0.aar\7eca742b05d3ca93c5f22855deb68690\jars\classes.jar;C:\Users\Owner\.gradle\caches\transforms-1\files-1.1\appcompat-v7-28.0.0.aar\b5690672012030cd411a187af3fc56e5\res;C:\Users\Owner\.gradle\caches\transforms-1\files-1.1\appcompat-v7-28.0.0.aar\b5690672012030cd411a187af3fc56e5\jars\classes.jar;C:\Users\Owner\.gradle\caches\modules-2\files-2.1\com.android.support.constraint\constraint-layout-solver\1.1.3\bde0667d7414c16ed62d3cfe993cff7f9d732373\constraint-layout-solver-1.1.3.jar;C:\Users\Owner\.gradle\caches\transforms-1\files-1.1\runtime-1.1.1.aar\b33f18eb28c72c82424fc1a72a917596\jars\classes.jar;C:\Program Files\Android\Android Studio\lib\idea_rt.jar" com.intellij.rt.execution.application.AppMainV2 com.example.owner.introductoryapplication.Test
Exception in thread "main" java.lang.RuntimeException: Stub!
at android.content.Context.<init>(Context.java:67)
at android.content.ContextWrapper.<init>(ContextWrapper.java:30)
at android.view.ContextThemeWrapper.<init>(ContextThemeWrapper.java:40)
at android.app.Activity.<init>(Activity.java:643)
at android.support.v4.app.SupportActivity.<init>(ComponentActivity.java:46)
at android.support.v4.app.FragmentActivity.<init>(FragmentActivity.java:68)
at android.support.v7.app.AppCompatActivity.<init>(AppCompatActivity.java:62)
at com.example.owner.introductoryapplication.Test.<init>(Test.java:13)
at com.example.owner.introductoryapplication.Test.main(Test.java:15)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.intellij.rt.execution.application.AppMainV2.main(AppMainV2.java:131)
Process finished with exit code 1
Though, from what I understand, I have already included the .model file in my assets folder; it should recognize the file path.
So this begs the questions: How would I enable Android Studio to process my PGBD_DecisionTable.model file and how would I use it with testing data (directly on the phone)?
Use .open("PGBD_DecisionTable.model") instead of .open(PGBD_DecisionTable.model).
You are trying to load a file from a constant String expression, not a static property of a class named PGBD_DecisionTable.
I want to use rgp to do a symbolic regression task. I am executing R script from Java using ScriptEngineEx. The R code works perfectly from Rsudio. However, just as I try loading the library rgp in java, I get an error message that "there is no package called 'rgp'".
First thing I tried was using lib.loc parameter for the library() function but got the same error message of not finding rgp.
Second thing I tried was loading other libraries from the same directory as rgp (emoa (also did not work), and manipulate(was successful)). I can not understand why it worked for some packages and not for the others in the same directory
Here is my code:
import org.rosuda.jrs.ScriptEngineEx;
import javax.script.ScriptEngineManager;
import javax.script.Invocable;
import javax.script.ScriptException;
public class RScript {
public static void main(String[] args) throws NoSuchMethodException, ScriptException {
// TODO Auto-generated method stub
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngineEx engine = (ScriptEngineEx) manager.getEngineByExtension("R");
engine.eval("library(manipulate)");
engine.eval("library(emoa)");
engine.eval("library(rgp)");
engine.eval("library(rgp, lib.loc =.libPaths())");
engine.close();
}
The error message that I get:
Error in library(emoa) : there is no package called 'emoa'
Error in library(rgp) : there is no package called 'rgp'
Error in library(rgp, lib.loc = .libPaths()) :
there is no package called 'rgp'
PS:
rgp, manipulate, and emoa are installed in directory:
C:\Users\userName\Documents\R\win-library\3.1
the value of .libpaths() is:
[1] "C:/Users/userName/Documents/R/win-library/3.1"
[2] "C:/Program Files/R/R-3.1.2/library"
both stmts: library(rgp) and library(rgp, lib.loc =.libPaths()) work fine from rStuido.
I hope you can direct me to further inspection to know why loading worked for some and not other libraries thought they are installed in the same location in .libPaths(). And any hint about how to load rgp would be much appreciated.
I'm using this code:
for (final String code : Locale.getISOCountries())
{
//stuff here
}
But on compile I get this error:
[ERROR] Line 21: No source code is available for type java.util.Locale; did you forget to inherit a required module?
And then a stack trace of compiler errors.
I'm doing both of these imports at the beginning of the class:
package com.me.example;
import java.util.Locale;
import java.util.*;
What can be wrong?
In Netbeans i see the autocomplete options and no syntax error for the Locale object...
Something screwy with your setup, the folllowing program works fine for me.
import java.util.*;
import java.util.Locale;
public class Donors {
public static void main (String [] args) {
for (final String code : Locale.getISOCountries()) {
System.out.println (code);
}
}
}
The fact that it's asking for source code leads me to believe that it's trying to compile or run it in some sort of debugging mode. You shouldn't need the source code for java.util.* to compile, that's just bizarre.
See if my simple test program works in your environment, then try looking for something along those lines (debugging options). Final step: compile your code with the baseline javac (not NetBeans).
UPDATE:
Actually, I have found something. If you are creating GWT applications, I don't think java.util.Locale is available on the client side (only the server side). All of the references on the web to this error message point to GWT and its limitations on the client side which are, after all, converted to Javascript goodies, so cannot be expected to support the entire set of Java libraries.
This page here shows how to do i18n on GWT apps and there's no mention of java.util.Locale except on the server side.
Looks like there might be something fishy in your build environment, as Locale.getISOCountries() should work just fine. Try compiling a small test program manually and see if you get the same error.
Definitely try to boil this down to a minimum, three-line program (or so), compile from the command-line, then put that class into your IDE and see if you still get the error, and if not, then change/add one line at a time until you have the original failing program, looking for what causes the problem. I'm thinking maybe some other import in your code is importing a Locale class? Why in the world would it be looking for source code?
See what happens when you compile this from the command-line:
import java.util.*;
public class LocaleTest {
public static void main(String[] args) {
Locale.getISOCountries();
}
}