DrJava scriptengine doesn't work - scriptengine

I try to evaluate math expression in string form using scriptengine, but it throws me NullPointerException
java.lang.NullPointerException
at Math.main(Math.java:12)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at edu.rice.cs.drjava.model.compiler.JavacCompiler.runCommand(JavacCompiler.java:272)
Here is the code:
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
import java.util.logging.Level;
import java.util.logging.Logger;
public class Math{
public static void main(String[] args){
ScriptEngineManager mgr = new ScriptEngineManager();
ScriptEngine jsEngine = mgr.getEngineByName("JavaScript");
try {
System.out.println("scriptEngine result: "+jsEngine.eval("{2+4*3*[1+(1+2+4)]}"));
} catch (ScriptException ex) {
Logger.getLogger(Math.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
i tried the same code in netbeans and it works, but i also need it to work in DrJava development environment.
Is it possible to resolve this or is there any other library that can evaluate string like this with combination of all parenthesis "{[()]}"?

Related

Exception in Aplication start method -- JavaFx with Intellj

I am trying to learn how to code with JavaFx but every time I try to create an object (ex.: WebView webview = new Webview();) I get this error:
ERROR-START{
Exception in Application start method
java.lang.reflect.InvocationTargetException at
java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native
Method) at
java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at
java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:567) at
javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:464)
at
javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:363)
at
java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native
Method) at
java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at
java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:567) at
java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:1051)
Caused by: java.lang.RuntimeException: Exception in Application start
method at
javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:900)
at
javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(LauncherImpl.java:195)
at java.base/java.lang.Thread.run(Thread.java:830) Caused by:
java.lang.IllegalAccessError: superclass access check failed: class
com.sun.javafx.sg.prism.web.NGWebView (in unnamed module #0x7dab7530)
cannot access class com.sun.javafx.sg.prism.NGGroup (in module
javafx.graphics) because module javafx.graphics does not export
com.sun.javafx.sg.prism to unnamed module #0x7dab7530 at
java.base/java.lang.ClassLoader.defineClass1(Native Method) at
java.base/java.lang.ClassLoader.defineClass(ClassLoader.java:1016) at
java.base/java.security.SecureClassLoader.defineClass(SecureClassLoader.java:151)
at
java.base/jdk.internal.loader.BuiltinClassLoader.defineClass(BuiltinClassLoader.java:821)
at
java.base/jdk.internal.loader.BuiltinClassLoader.findClassOnClassPathOrNull(BuiltinClassLoader.java:719)
at
java.base/jdk.internal.loader.BuiltinClassLoader.loadClassOrNull(BuiltinClassLoader.java:642)
at
java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:600)
at
java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
at AppL.start(AppL.java:8) at
javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:846)
at
javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:455)
at
javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:428)
at
java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
at
javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:427)
at
javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
at
javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native
Method) at
javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:174)
... 1 more Exception running application AppL
}ERROR-ENDING
I've tried to add: (I also added the PATH_TO_FX Path-variable)
--module-path ${PATH_TO_FX} --add-modules javafx.controls,javafx.fxml
to the run configurations VM-options but it only changed the error I had before this error. I tried to do this on Windows AND on Linux, neither works.
Intellj shows me that every thing works, but it doesn't.
I wanted to use netbeans or eclipse but I don't know how to add libraries to them.
That would helb me to.
Example code:
import javafx.application.Application;
import javafx.scene.web.WebView;
import javafx.stage.Stage;
public class AppL extends Application {
#Override
public void start(Stage stage) throws Exception {
WebView v = new WebView();
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
Thanks to anyone who wants to help me, and who doesn't lauph on my english-scills😊

While creating sequenceFile getting ERROR nativeio.NativeIO: Unable to initialize NativeIO libraries

I am using standalone spark and while running a program which writes a sequence file using the pair RDD and I am getting below error:
ERROR nativeio.NativeIO: Unable to initialize NativeIO libraries
java.lang.NoSuchFieldError: workaroundNonThreadSafePasswdCalls
at org.apache.hadoop.io.nativeio.NativeIO.initNative(Native Method)
at org.apache.hadoop.io.nativeio.NativeIO.<clinit>(NativeIO.java:58)
at org.apache.hadoop.fs.FileUtil.setPermission(FileUtil.java:653)
at org.apache.hadoop.fs.RawLocalFileSystem.setPermission(RawLocalFileSystem.java:509)
at org.apache.hadoop.fs.FilterFileSystem.setPermission(FilterFileSystem.java:286)
at org.apache.hadoop.fs.ChecksumFileSystem.create(ChecksumFileSystem.java:385)
at org.apache.hadoop.fs.ChecksumFileSystem.create(ChecksumFileSystem.java:364)
at org.apache.hadoop.fs.FileSystem.create(FileSystem.java:555)
at org.apache.hadoop.io.SequenceFile$Writer.<init>(SequenceFile.java:892)
at org.apache.hadoop.io.SequenceFile.createWriter(SequenceFile.java:393)
at org.apache.hadoop.io.SequenceFile.createWriter(SequenceFile.java:354)
at org.apache.hadoop.io.SequenceFile.createWriter(SequenceFile.java:476)
at org.apache.hadoop.mapred.SequenceFileOutputFormat.getRecordWriter(SequenceFileOutputFormat.java:58)
at org.apache.spark.SparkHadoopWriter.open(SparkHadoopWriter.scala:89)
at org.apache.spark.rdd.PairRDDFunctions$$anonfun$13.apply(PairRDDFunctions.scala:980)
at org.apache.spark.rdd.PairRDDFunctions$$anonfun$13.apply(PairRDDFunctions.scala:974)
at org.apache.spark.scheduler.ResultTask.runTask(ResultTask.scala:62)
at org.apache.spark.scheduler.Task.run(Task.scala:54)
at org.apache.spark.executor.Executor$TaskRunner.run(Executor.scala:177)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Following is the the code that I am using:
import java.util.ArrayList;
import java.util.List;
import scala.Tuple2;
import org.apache.spark.api.java.JavaPairRDD;
import org.apache.spark.api.java.JavaSparkContext;
import org.apache.spark.api.java.function.PairFunction;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapred.SequenceFileOutputFormat;
public class BasicSaveSequenceFile {
public static class ConvertToWritableTypes implements PairFunction<Tuple2<String, Integer>, Text, IntWritable> {
public Tuple2<Text, IntWritable> call(Tuple2<String, Integer> record) {
return new Tuple2(new Text(record._1), new IntWritable(record._2));
}
}
public static void main(String[] args) throws Exception {
if (args.length != 2) {
throw new Exception("Usage BasicSaveSequenceFile [sparkMaster] [output]");
}
String master = args[0];
String fileName = args[1];
JavaSparkContext sc = new JavaSparkContext(
master, "basicloadsequencefile", System.getenv("SPARK_HOME"), System.getenv("JARS"));
List<Tuple2<String, Integer>> input = new ArrayList();
input.add(new Tuple2("coffee", 1));
input.add(new Tuple2("coffee", 2));
input.add(new Tuple2("pandas", 3));
JavaPairRDD<String, Integer> rdd = sc.parallelizePairs(input);
JavaPairRDD<Text, IntWritable> result = rdd.mapToPair(new ConvertToWritableTypes());
result.saveAsHadoopFile(fileName, Text.class, IntWritable.class, SequenceFileOutputFormat.class);
}
}
I have added all the dependencies in pom.xml. Can you please help?
I upgraded the version of spark_core jar to 1.3.1 from 1.1.0 in pom.xml. Also, removed dependency on hadoop-common jar from pom.xml.
It worked for me!!!

Can not read excel file in javafx when it converted to JAR

I am using Apache POI for reading excel file (in xls and xlsx format).
It runs fine in IDE (I am using IntelliJ), but when I make the project to JAR, it can't read the excel file. I tried by giving full path of excel file, but not working.
File name spelling is right, file is not protected or read only format. Everything is good until I run JAR.
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(Unknown Source)
at com.sun.javafx.application.LauncherImpl.launchApplication(Unknown Source)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at sun.launcher.LauncherHelper$FXHelper.main(Unknown Source)
Caused by: java.lang.NoClassDefFoundError: org/apache/poi/hssf/usermodel/HSSFWorkbook
at TakeInput.Take(TakeInput.java:25)
at MainGUI.main(MainGUI.java:53)
... 11 more
Caused by: java.lang.ClassNotFoundException: org.apache.poi.hssf.usermodel.HSSFWorkbook
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 13 more
Exception running application MainGUI
Here is my TakeInput Class:
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import java.io.*;
import java.util.Arrays;
import java.util.Comparator;
import java.util.StringTokenizer;
public class TakeInput {
public void Take() throws Exception{
System.out.println("Inside TakeInput class..");
File f = new File("courses.xls");
FileInputStream fs = new FileInputStream(f);
HSSFWorkbook wb = new HSSFWorkbook(fs);
HSSFSheet sheet = wb.getSheetAt(0);
int CSECourseCounter=0;
for(int i=0;i<=sheet.getLastRowNum();i++){
String tempCode = sheet.getRow(i).getCell(0).getStringCellValue();
StringTokenizer tok = new StringTokenizer(tempCode);
String codeTok=tok.nextToken();
if(codeTok.equals("CSE") || codeTok.equals("CSI")) {
CSECourseCounter++;
}
}
CourseInfo[] courses = new CourseInfo[CSECourseCounter];
for(int i=0;i<CSECourseCounter;i++)
courses[i]=new CourseInfo();
System.out.println(sheet.getLastRowNum());
for(int i=0,j=0;i<=sheet.getLastRowNum();i++){
String tempCode = sheet.getRow(i).getCell(0).getStringCellValue();
StringTokenizer tok = new StringTokenizer(tempCode);
String codeTok=tok.nextToken();
if(codeTok.equals("CSE") || codeTok.equals("CSI")) {
courses[j].code = sheet.getRow(i).getCell(0).getStringCellValue();
courses[j].title = sheet.getRow(i).getCell(1).getStringCellValue();
courses[j].section = sheet.getRow(i).getCell(2).getStringCellValue();
courses[j].day = sheet.getRow(i).getCell(4).getStringCellValue();
courses[j].time = sheet.getRow(i).getCell(5).getStringCellValue();
//courses[i].room = sheet.getRow(i).getCell(6).getNumericCellValue();
//System.out.println(courses[j].code);
j++;
//CSECourseCounter++;
}
}
Arrays.sort(courses, new Comparator<CourseInfo>() {
#Override
public int compare(CourseInfo o1, CourseInfo o2) {
return o1.title.compareTo(o2.title);
}
});
System.out.println("CSE courses = "+CSECourseCounter);
for(int i=0;i<CSECourseCounter;i++){
courses[i].setCampus();
courses[i].setLabCLassAndTimeSlot();
courses[i].setDay();
}
//***************************** CourseInfo to a linked list ******************
for(int i=0;i<courses.length;i++){
CourseToLinkedList temp = AddSearchCourse.searchCourse(courses[i].code);
if(temp==null) {
AddSearchCourse.addCourse(courses[i].title,courses[i].code,courses[i].islabClass);
CourseToLinkedList temp1 = AddSearchCourse.searchCourse(courses[i].code);
temp1.addSections(courses[i].code,courses[i].title,courses[i].section,courses[i].day,courses[i].time,
courses[i].timeStart,courses[i].timeEnd,courses[i].room,
courses[i].faculty,courses[i].credit,courses[i].assigned,
courses[i].campus,courses[i].dept,courses[i].islabClass,
courses[i].timeSlot,courses[i].labTimeSlot,courses[i].day1,courses[i].day2
);
}
else{
CourseToLinkedList temp1 = AddSearchCourse.searchCourse(courses[i].code);
temp1.addSections(courses[i].code,courses[i].title,courses[i].section,courses[i].day,courses[i].time,
courses[i].timeStart,courses[i].timeEnd,courses[i].room,
courses[i].faculty,courses[i].credit,courses[i].assigned,
courses[i].campus,courses[i].dept,courses[i].islabClass,
courses[i].timeSlot,courses[i].labTimeSlot,courses[i].day1,courses[i].day2
);
}
}
System.out.println("outside try catch..");
}
}
I put a copy of this excel file which is in the project folder to that folder where JAR file situated. No problem with location.
As I can see the logs, this issue may happen because of dependency jars for your code. try to add the jar(may be 'jakarta-poi-version.jar') having class 'HSSFWorkbook' into your class path and then try to run it again.

Null pointer exception on Initialize second controller class from first controller class

I am trying to access initialize method of another calss from a different class.But I am getting nullpointer Exception while returning ctroller
THis is the code I am trying to call from my second calss.
Line1- FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/src/myjavfxapp/controller/EditClientDetails.fxml"));
Line 2- EditClientDetailsController fooController = (EditClientDetailsController) fxmlLoader.getController();
Line3- fooController.initialize(null, null);
I am getting null pointer exception at line number 3.
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at sun.reflect.misc.Trampoline.invoke(MethodUtil.java:75)
at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:279)
at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1435)
... 44 more
Caused by: java.lang.NullPointerException
at myjavfxapp.controller.NewUserController.saveNewuser(NewUserController.java:167)
... 54 more
My Intention is to initialize the "EditClientDetails.fxml" fields from different controller class.
Please point out if i missed anything.
Try the below example it working fine, you have to use load() function of FxmlLoader class.
public class Xxx extends Application {
#Override
public void start(Stage stage) throws Exception {
//Parent root = FXMLLoader.load(getClass().getResource("Sample.fxml"));
FXMLLoader loader = new FXMLLoader(getClass().getResource("Sample.fxml"));
Parent root = (Parent)loader.load();
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
SampleController controller = (SampleController)loader.getController();
}
public static void main(String[] args) {
launch(args);
}
}

SMB connection with java

Hi i have created a test program to connect with SMB protocol. My motive is to create a test.txt file on a shared location like String path="smb://192.168.143.134/rtf2xml/"+sharedFolder+"/test.txt";
But when I try to run my program (below is the code sample)
import java.io.IOException;
import jcifs.smb.NtlmPasswordAuthentication;
import jcifs.smb.SmbFile;
import jcifs.smb.SmbFileOutputStream;
public class Test {
/**
* #param args
* #throws IOException
*/
public static void main(String[] args) throws IOException {
String user = "abc";
String pass ="123456";
String sharedFolder="INPUT";
String path="smb://192.168.143.134/rtf2xml/"+sharedFolder+"/test.txt";
NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication("192.168.143.134",user, pass);
SmbFile smbFile = new SmbFile(path, auth);
smbFile.createNewFile();
SmbFileOutputStream smbfos = new SmbFileOutputStream(smbFile);
smbfos.write("testing....and writing to a file".getBytes());
System.out.println("completed ...nice !");
}
}
It is throwing exception
Exception in thread "main" jcifs.smb.SmbException: Failed to negotiate
jcifs.smb.SmbException: Timeout trying to open socket
java.net.ConnectException: Connection refused: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at jcifs.netbios.NbtSocket.<init>(NbtSocket.java:59)
at jcifs.smb.SmbTransport.run(SmbTransport.java:342)
at java.lang.Thread.run(Unknown Source)
at jcifs.smb.SmbTransport.start(SmbTransport.java:315)
at jcifs.smb.SmbTransport.negotiate0(SmbTransport.java:865)
at jcifs.smb.SmbTransport.negotiate(SmbTransport.java:941)
at jcifs.smb.SmbTree.treeConnect(SmbTree.java:119)
at jcifs.smb.SmbFile.connect(SmbFile.java:827)
at jcifs.smb.SmbFile.connect0(SmbFile.java:797)
at jcifs.smb.SmbFile.open0(SmbFile.java:852)
at jcifs.smb.SmbFile.createNewFile(SmbFile.java:2265)
at Test.main(Test.java:22)
at jcifs.smb.SmbTransport.negotiate(SmbTransport.java:947)
at jcifs.smb.SmbTree.treeConnect(SmbTree.java:119)
at jcifs.smb.SmbFile.connect(SmbFile.java:827)
at jcifs.smb.SmbFile.connect0(SmbFile.java:797)
at jcifs.smb.SmbFile.open0(SmbFile.java:852)
at jcifs.smb.SmbFile.createNewFile(SmbFile.java:2265)
at Test.main(Test.java:22)
How to get rid of this?
But l think you have to make sure that the destination server(192.168.143.134) is up and alive.
you can write it the following way, since the IP in included in the smb link.
public static void main(String[] args) throws IOException {
String user = "abc";
String pass ="123456";
String sharedFolder="INPUT";
String path="smb://192.168.143.134/rtf2xml/"+sharedFolder+"/test.txt";
NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication("",user, pass);//note here
SmbFile smbFile = new SmbFile(path, auth);
smbFile.createNewFile();
SmbFileOutputStream smbfos = new SmbFileOutputStream(smbFile);
smbfos.write("testing....and writing to a file".getBytes());
System.out.println("completed ...nice !");
}
....

Resources