I have the following code to compile scala code at runtime in a Java program
Settings s = new Settings();
Global g = new Global(s);
Global.Run run = g.new Run();
List<String> files = new LinkedList<>();
files.add("src/main/java/scala/rules/ScalaRuleBasedStrategy.scala");
run.compile(JavaConverters.asScalaBufferConverter(files)
.asScala().toList());
But I am getting this error:
Exception in thread "main" java.lang.NoSuchMethodError: scala.tools.nsc.Global$gen$.mkBlock(Lscala/collection/immutable/List;)Lscala/reflect/internal/Trees$Tree;
at scala.tools.nsc.ast.parser.TreeBuilder.makeBlock(TreeBuilder.scala:110)
at scala.tools.nsc.ast.parser.Parsers$Parser.block(Parsers.scala:1689)
Try
Settings s = new Settings();
MutableSettings.BooleanSetting usejavacp = s.usejavacp(); // added
usejavacp.value_$eq(true); // added
Global g = new Global(s);
Global.Run run = g.new Run();
List<String> files = new LinkedList<>();
files.add("src/main/java/scala/rules/ScalaRuleBasedStrategy.scala");
run.compile(
JavaConverters.asScalaBufferConverter(files)
.asScala().toList()
);
Related
Im trying load descriptors from external source.
When using generated files without imports it works without problem but when proto have imports then while building FileDescriptor I gets DescriptorValidationException:
Failed to parse descriptor ./descriptors/test.dsc
com.google.protobuf.Descriptors$DescriptorValidationException: AccessRequest.date_from: ".google.protobuf.Timestamp" is not defined.
Proto file which i use to get *.dsc file:
syntax = "proto3";
import "google/protobuf/timestamp.proto";
message AccessRequest {
int64 cabinet_id = 1;
google.protobuf.Timestamp date_from = 2;
google.protobuf.Timestamp date_to = 3;
}
Command which I use to get *.dsc:
protoc --include_imports --proto_path=src/main/proto/ --descriptor_set_out=descriptors/test.dsc src/main/proto/test.proto
My code to load *.dsc files:
try (final InputStream stream = Files.newInputStream(path)) {
final FileDescriptorSet fds = FileDescriptorSet.parseFrom(stream);
final TypeRegistry.Builder builder = TypeRegistry.newBuilder();
for (final FileDescriptorProto fdp : fds.getFileList()) {
final FileDescriptor fd = FileDescriptor.buildFrom(fdp, new FileDescriptor[]{});
builder.add(fd.getMessageTypes());
globalBuilder.add(fd.getMessageTypes());
}
return new Queue(base, builder.build());
}
FileDescriptor.buildFrom(fdp, new FileDescriptor[]{}) is the key point.
You should build the dependency proto(import) recursively, and take the result to replace the second parameter.
Give you my code for reference
private FileDescriptor buildFileDescriptor(FileDescriptorProto currentFileProto,
Map<String, FileDescriptorProto> fileProtoCache) {
List<FileDescriptor> dependencyFileDescriptorList = new ArrayList<>();
currentFileProto.getDependencyList().forEach(dependencyStr -> {
FileDescriptorProto dependencyFileProto = fileProtoCache.get(dependencyStr);
FileDescriptor dependencyFileDescriptor = buildFileDescriptor(dependencyFileProto, fileProtoCache);
dependencyFileDescriptorList.add(dependencyFileDescriptor);
});
try {
return FileDescriptor.buildFrom(currentFileProto, dependencyFileDescriptorList.toArray(new FileDescriptor[0]));
} catch (DescriptorValidationException e) {
throw new IllegalStateException("FileDescriptor build fail!", e);
}
}
I know it's late but incase someone is looking to parse a .desc file with imports, this seemed to work for me:
FileInputStream fin = new FileInputStream(descPath);
DescriptorProtos.FileDescriptorSet set = DescriptorProtos.FileDescriptorSet.parseFrom(fin);
List<FileDescriptor> dependencyFileDescriptorList = new ArrayList<>();
for(int i=0; i<set.getFileCount()-1;i++) {
dependencyFileDescriptorList.add(Descriptors.FileDescriptor.buildFrom(set.getFile(i), depndencyFileDescriptorList.toArray(new FileDescriptor[i]));
}
FileDescriptor desc = Descriptors.FileDescriptor.buildFrom(set.getFile(set.getFileCount()-1), dependencyFileDescriptorList.toArray(new FileDescriptor[0]));
I want to use JLine to build a simple CLI. But I ran into a problem. File name completer doesn't work properly on Windows.
When I enter 8> first C:\ and press tab, completer do nothing although it should display all subfolders.
Here is my code:
ArgumentCompleter completer1 = new ArgumentCompleter(
new StringsCompleter("first"),
new FileNameCompleter(),
new NullCompleter()
);
ArgumentCompleter completer2 = new ArgumentCompleter(
new StringsCompleter("second"),
new NullCompleter()
);
AggregateCompleter completer3 = new AggregateCompleter(
completer1, completer2
);
ConsoleReader console = new ConsoleReader();
console.addCompleter(completer3);
while (true) {
String line = console.readLine("8> ");
if (line.isEmpty()){
console.println();
}
}
Any ideas how to fix this?
I have problems accessing Java classes in JavaScript. Calling a code snippet
var String = Java.type("java.lang.String");
from Java via javax.script.ScriptEngine, yields follwing Error
Exception in thread "main" javax.script.ScriptException: sun.org.mozilla.javascript.internal.EcmaError: ReferenceError: "Java" is not defined. (path/to/string.js#1) in path/to/string.js at line number 1
at com.sun.script.javascript.RhinoScriptEngine.eval(RhinoScriptEngine.java:156)
at main.JsTest.main(JsTest.java:55)
Using non-Java classes in the script works fine, e.g. var value = a + b, where a and b are defined in a javax.script.ScriptContext.
This is the Java class that executes the script.
JsTest.java
public class JsTest
{
public static void main(String[] args) throws Exception
{
ScriptEngineManager sem = new ScriptEngineManager();
ScriptEngine se = sem.getEngineByExtension("js");
String script = "path/to/string.js";
File scriptFile = new File(script);
FileReader fr = new FileReader(scriptFile);
se.put(ScriptEngine.FILENAME, script);
ScriptContext sc = new SimpleScriptContext();
se.eval(fr, sc);
}
}
I have no idea where your Java.type is coming from, but the official documentation uses Packages.java or just java.
So your line should probably look like
var String = Packages.java.lang.String;
public class Evaluate {
public static void main(String args[]) throws Exception{
//load datasets
DataSource source = new DataSource("F:/data/solar_flare.arff");
Instances dataset = source.getDataSet();
BufferedReader reader = new BufferedReader(new FileReader("F:/data/solar_flare.arff"));
Instances training = new Instances(reader);
reader = new BufferedReader(new FileReader("F:/data/solar_flare.arff"));
Instances testing = new Instances(reader);
J48 jjjj = new J48();
MultilabelClassifier PS = new PS();
PS.setClassifier(jjjj);
PS.setOptions(Utils.splitOptions("-threshold PCut1 -verbosity 3"));
PS.buildClassifier(training);
Evaluation ecal = new Evaluation(training);
ecal.evaluateModel(PS, testing);
dataset.setClassIndex(dataset.numAttributes()-1);
J48 tree = new J48();
tree.buildClassifier(dataset);
Evaluation eval = new Evaluation(dataset);
Random rand = new Random(1);
int folds = 10;
DataSource source1 = new DataSource("F:/data/solar_flare.arff");
Instances testDataset = source1.getDataSet();
testDataset.setClassIndex(testDataset.numAttributes()-1);
eval.crossValidateModel(tree, testDataset, folds, rand);
System.out.println(eval.toMatrixString("=== Confusion Matrix ===\n"));
}
}
This my Code its give error back
This is error
"Exception in thread "main" java.lang.NoSuchMethodError:
weka.core.Attribute.(Ljava/lang/String;Ljava/util/List;)V at
meka.core.PSUtils.PSTransformation(PSUtils.java:416) at
meka.classifiers.multilabel.PS.buildClassifier(PS.java:225)" at
Evaluate.main(Evaluate.java:57)
This is a typical error when you are using a library with a version that does not correspond to what you expect. You have to make sure the weka.core.Attribute exists in your version with the specified method.
I have a question that has been boring me for some days.
I'm implementing some swing interfaces that is supposed to load an image:
When i press a button it triggers an event that calls the following method:
private void GetDataPanelGeral()
{
ArrayList<String> Files = new ArrayList<>();
java.util.Date selectedDate = (java.util.Date) this.JDatePickerImpl_GERAL_MUDANÇAS_datePicker_START.getModel().getValue();
java.sql.Date sqlDateINICIO = new java.sql.Date(selectedDate.getTime());
selectedDate = (java.util.Date) this.JDatePickerImpl_GERAL_MUDANÇAS_datePicker_END.getModel().getValue();
java.sql.Date sqlDateFIM = new java.sql.Date(selectedDate.getTime());
//CALL MODULE 5
RunningLoop loop = null;
Thread t = new Thread
(
loop = new RunningLoop(50000,this.StringDBName,this.StringDBDriver,
this.StringDBUser,this.StringDBPass,this.PathToData,
sqlDateINICIO.toString(),
sqlDateFIM.toString(),
true)
);
t.start();
loop.StopLooping();
this.ImageLabelGERALMUNDANCAS.revalidate();
this.ImageLabelGERALMUNDANCAS.repaint();
this.ImageLabelGERALMUNDANCAS = new JLabel(new ImageIcon("this.PathToData+"/Results/Outputs/P5/0.png",""));
this.ImageLabelGERALMUNDANCAS.repaint();
JPanel Aggregator = new JPanel();
Aggregator.add(this.ImageLabelGERALMUNDANCAS);
Aggregator.setBackground(this.color);
//this.Aggregatorr.add(this.ImageLabelGERALMUNDANCAS);
this.JPanel_TABGeral.remove(1);
this.JPanel_TABGeral.add(Aggregator,1);
this.JPanel_TABGeral.revalidate();
this.JPanel_TABGeral.repaint();
Aggregator.revalidate();
Aggregator.repaint();
}
the image loaded in the line this.ImageLabelGERALMUNDANCAS = new JLabel(new ImageIcon("this.PathToData+"/Results/Outputs/P5/0.png","")); is created by a module called in the lines
//CALL MODULE 5
RunningLoop loop = null;
Thread t = new Thread
(
loop = new RunningLoop(50000,this.StringDBName,this.StringDBDriver,
this.StringDBUser,this.StringDBPass,this.PathToData,
sqlDateINICIO.toString(),
sqlDateFIM.toString(),
true)
);
t.start();
loop.StopLooping();
it seems to be all ok but it does not load the image.. If i try to load another image it works perfectly so i think the problem is the creation of the image in the thread.
Can somebody help me?
Ps: I'm still need to work in some code, currently i'm trying everything to get this work, then i'll worry about better code
Kind regards