error: cannot find symbol (using replaceAll ) codenameone - java

I´m want to replace a String expression and I receive this error message
error: cannot find symbol
test1 = testw.replaceAll("/uploads","http://www.anbariloche.com.ar/uploads");
symbol: method replaceAll(String,String)
location: variable testw of type String
this is my code
String testw= String.valueOf(element1);
String test1;
test1 = testw.replaceAll("/uploads","http://www.anbariloche.com.ar/uploads");
I use Netbeans 8.1
Product Version: NetBeans IDE 8.1 (Build 201510222201)
Java: 1.7.0_79; Java HotSpot(TM) Client VM 24.79-b02
Runtime: Java(TM) SE Runtime Environment 1.7.0_79-b15
System: Windows 7 version 6.1 running on x86; Cp1252; es_ES (nb)
The complete code updated
#Override
protected void beforePortada(Form f) {
WebBrowser browser=new WebBrowser();
f.setLayout(new BorderLayout());
f.addComponent(BorderLayout.CENTER, browser);
/////Parse
String URL= "http://www.anbariloche.com.ar/";
ConnectionRequest req = new ConnectionRequest();
req.setUrl(URL);
NetworkManager.getInstance().addToQueueAndWait(req);
byte[] data = req.getResponseData();
if (data == null) {
}
XMLParser xmlParser=new XMLParser();
Element element= null;
try {
element = xmlParser.parse(new InputStreamReader(new ByteArrayInputStream(data), "UTF-8"));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
Element element1=element.getChildAt(0);
String testw= String.valueOf(element1);
///replace the string
testw = testw.replaceAll("/uploads/","http://www.anbariloche.com.ar/uploads/");
browser.setPage(testw,null);
}
}
That's the code updated, I can see where is my mistake
with the String

Use StringUtil.replaceAll(string, pattern, replace).
Implementing this in the VM layer is much harder to do in a truly portable way and so we nudge you towards the more portable versions.

There will not be any error in netbean while coding and it shows runtime error so StringUtil should be imported and called its replaceAll static method
eg: StringUtil.replaceAll

Related

Java-wrapped Matlab function: `java` can't load/find `main` sample invoker class

Java-wrapped Matlab function: java can't load/find main sample invoker class
I'm following a MATLAB example of wrapping a MATLAB function in a Java interface. The sample driver (i.e., invoker of the wrapped function) compiles without errors or any messages, but java says that it can't find/load the main class, i.e., the sample driver.
The MATLAB function to be wrapped is exactly as it is on the web page (and in fact, it comes with the MATLAB installation):
" makesqr.m
"----------
function y = makesqr(x)
y = magic(x);
The sample invoker is extremely simple:
" makesqrSample1.m
"-----------------
% Sample script to demonstrate execution of function y = makesqr(x)
x = 3; % Initialize x here
y = makesqr(x);
Everything is exactly as shown in the webpage. I get all the files described in this file summary.
Things start to depart from expected in the "Install and Implement MATLAB Generated Java Application" section. Step 3 refers to a sample invoker getmagic.java instead of the makesqrSample1.java (automagically generated by MATLAB from makesqrSample1.m above). I assume that this is a typo.
With makesqr.jar and makesqrSample1.java in the same (current working) directory, the following compilation issues no messages or errors.
javac -cp \
"makesqr.jar;C:\Program Files\MATLAB\R2019a\toolbox\javabuilder\jar\javabuilder.jar" \
makesqrSample1.java
This creates makesqrSample1.class in the same folder. Here is the error from execution:
java -cp \
"makesqr.jar;C:\Program Files\MATLAB\R2019a\toolbox\javabuilder\jar\javabuilder.jar" \
makesqrSample1
Error: Could not find or load main class makesqrSample1
I checked that the that auto-generated makesqrSample1.java does have main (see ANNEX below).
This is a minimal example, following the documentation faithfully. What is causing main to not be recognized?
CONTEXTUAL DETAILS
Version output (select details):
MATLAB Version: 9.6.0.1072779 (R2019a)
Operating System: Microsoft Windows 10 Pro Version 10.0 (Build 18362)
Java Version: Java 1.8.0_181-b13 with Oracle Corporation Java HotSpot(TM) 64-Bit Server VM mixed mode
MATLAB Compiler Version 7.0.1 (R2019a)
MATLAB Compiler SDK Version 6.6.1 (R2019a)
Installed JDK:
C:\Program Files\AdoptOpenJDK\jdk-8.0.265.01-hotspot
Since I have MATLAB installed, I didn't get the MATLAB Runtime (and from past experience, it has never been clear how/whether the Runtime is being used when MATLAB is installed). The the problem is occurring right up front finding/loading main.
ANNEX: AUTO-GENERATED makesqrSample1.java
import com.mathworks.toolbox.javabuilder.*;
import makesqr.Class1;
/**
*
* Sample driver code that is integrated with a compiled MATLAB function
* generated by MATLAB Compiler SDK.
*
* Refer to the MATLAB Compiler SDK documentation for more
* information.
*
* #see com.mathworks.toolbox.javabuilder.MWArray
*
*/
public class makesqrSample1 {
private static Class1 class1Instance;
private static void setup() throws MWException {
class1Instance = new Class1();
}
/**
* Sample code for {#link Class1#makesqr(int, Object...)}.
*/
public static void makesqrExample() {
MWArray xIn = null;
MWNumericArray yOut = null;
Object[] results = null;
try {
double xInData = 3.0;
xIn = new MWNumericArray(xInData, MWClassID.DOUBLE);
results = class1Instance.makesqr(1, xIn);
if (results[0] instanceof MWNumericArray) {
yOut = (MWNumericArray) results[0];
}
System.out.println(yOut);
} catch (Exception e) {
e.printStackTrace();
} finally {
// Dispose of native resources
MWArray.disposeArray(xIn);
MWArray.disposeArray(results);
}
}
public static void main(String[] args) {
try {
setup();
} catch (Exception e) {
e.printStackTrace();
System.exit(1);
}
try {
makesqrExample();
} catch (Exception e) {
e.printStackTrace();
System.exit(1);
} finally {
// Dispose of native resources
class1Instance.dispose();
}
}
}
This answer is definitely for the Java newbies. The class path for java needs to include the directory . of the newly-compiled makesqrSample1.class:
java -cp \
"makesqr.jar;C:\Program Files\MATLAB\R2019a\toolbox\javabuilder\jar\javabuilder.jar;." \
makesqrSample1
Running C:\cygwin64\tmp\User.Name\mcrCache9.6\makesq0\makesqr\startup
8 1 6
3 5 7
4 9 2
What I find odd is that this java is a Windows installation, yet it seems to recognize that I'm invoking it from Cygwin, and it creates a working folder in C:\cygwin64\tmp\User.Name.

How to input args in cmd/sudo and use them in runtime? (Java)

I have this code:
public class CalculatingApp {
public static void main(String[] args) {
AtomicInteger result = new AtomicInteger();
int valueA = Integer.parseInt(args[0]);
String operation = args[1];
int valueB = Integer.parseInt(args[2]);
if ("add".equals(operation)) {
result.set(valueA + valueB);
} else if ("subt".equals(operation)) {
result.set(valueA - valueB);
} else if ("mult".equals(operation)) {
result.set(valueA * valueB);
} else if ("div".equals(operation)) {
result.set(valueA / valueB);
} else {
System.out.println("Incorrect input");
}
System.out.println(result.get());
}
}
I use IDEa terminal for these commands:
javac CalculatingApp.java
java CalculatingApp 5 add 10
But console output:
Error: A JNI error has occurred, please check your installation and try again
Exception in thread "main" java.lang.UnsupportedClassVersionError: CalculatingApp has been compiled by a more recent version of the Java Runtime (class file version 57.0), this version
of the Java Runtime only recognizes class file versions up to 52.0
Java -version:
java version "1.8.0_51"
Java(TM) SE Runtime Environment (build 1.8.0_51-b16)
Java HotSpot(TM) 64-Bit Server VM (build 25.51-b03, mixed mode)
You may find other errors.
Looks the code is compiled by JAVA8, but ran by Java 13.
Try java -version in IDEA terminal to take a look the java version.

Integrating Groovy RootLoader with Java 8 SystemClassLoader

The following is deep inside a library I use. In 2015 this worked with Groovy 2.3 and early versions of 2.4, probably with Java 6 or 7! I wanted to update to Java 8 before trying to modify for Java9+.
final class DynamicClassLoader extends ClassLoader {
final NodeID originatingNode;
NetChannelOutput requestClassData;
NetChannelInput classDataResponse = NetChannel.net2one();
final Hashtable classes = new Hashtable();
DynamicClassLoader(NodeID originator, NetChannelLocation requestLocation) {
super(ClassLoader.getSystemClassLoader());
this.originatingNode = originator;
this.requestClassData = NetChannel.one2net(requestLocation);
}
...
}
When I try to invoke the code from Groovy I get the following error:
org.codehaus.groovy.tools.RootLoader cannot be cast to jcsp.net2.mobile.DynamicClassLoader
The Point where this is called from is given in the following code at the line indicated by **
public byte[] filterTX(Object obj)
throws IOException
{
ClassLoader loader = obj.getClass().getClassLoader();
byte[] bytes = this.internalFilter.filterTX(obj);
if (loader == ClassLoader.getSystemClassLoader() || loader == null)
{
DynamicClassLoaderMessage message = new DynamicClassLoaderMessage(Node.getInstance().getNodeID(),
(NetChannelLocation) ClassManager.in.getLocation(), bytes);
byte[] wrappedData = this.internalFilter.filterTX(message);
return wrappedData;
}
**DynamicClassLoader dcl = (DynamicClassLoader)loader;**
DynamicClassLoaderMessage message = new DynamicClassLoaderMessage(dcl.originatingNode,
(NetChannelLocation) ClassManager.in.getLocation(), bytes);
byte[] wrappedData = this.internalFilter.filterTX(message);
return wrappedData;
}
After discussion with the Groovy community I discoverd that the problem lay in the way the Intellij invokes Groovy scripts. The code works in Eclipse without any problem. In Intellij it was necessary to create a jar artifact for each of the scripts I wanted to run in parallel, which I could then run from a command line interface. I recoded the application in Java 8 and it worked with no problem. Hope that helps.

Cannot access CPCallbackClassLoaderIf / XMLable (class file not found, when using Java 8)

I've a strange problem, I cannot compile following code (with Java 8, with Java 6 I've no problems!).
I got following compile-time errors:
Compile Error 1:
error: cannot access CPCallbackClassLoaderIf
com.sun.javaws.jnl.JARDesc[] descs = jnlpcl.getLaunchDesc().getResources().getEagerOrAllJarDescs(true);
class file for com.sun.deploy.security.CPCallbackClassLoaderIf not found
and
Compile Error 2:
error: cannot access XMLable
com.sun.javaws.jnl.JARDesc[] descs = jnlpcl.getLaunchDesc().getResources().getEagerOrAllJarDescs(true);
class file for com.sun.deploy.xml.XMLable not found
NetBeans IDE shows no errors in this code!.
public static List<String> test() {
try {
List<String> ret = new ArrayList<String>();
ClassLoader cl = Thread.currentThread().getContextClassLoader();
if (cl instanceof com.sun.jnlp.JNLPClassLoader) {
com.sun.jnlp.JNLPClassLoader jnlpcl = (com.sun.jnlp.JNLPClassLoader)cl;
/*COMPILE ERROR1*/ com.sun.javaws.jnl.JARDesc[] descs = jnlpcl.getLaunchDesc().getResources().getEagerOrAllJarDescs(true);
for (com.sun.javaws.jnl.JARDesc d : descs)
{
/*COMPILE ERROR2*/ JarFile jf = jnlpcl.getJarFile(d.getLocation());
ret.add(new File(jf.getName()).getAbsolutePath());
}
}
return ret;
} catch (Exception ex) {
//ignore
}
return null;
}
I checked deploy.jar and indeed, this class does not exists anymore in Java 8 (but in Java 6).
Why does this error happens and how can I get rid of this error, when using this code and Java 8?

OutOfMemoryException (after upgrade to JDK 7u45)

After upgrading to the latest JDK, we've got (on some machines) a strange OutOfMemoryException.
Consider this simple application:
public class Test
{
public static void main (String[] args) {
try {
java.text.SimpleDateFormat dateFormatter = new java.text.SimpleDateFormat("E dd/MM/yyyy HH:mm");
System.out.println("formatted date: " + dateFormatter.format(new java.util.Date()));
} catch (Exception x) {
System.err.println(x);
System.exit(1);
}
}
}
Running this small program will result in this exception (even when running it with -Xmx2048M -Xms2048):
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
at java.util.Currency.readLongArray(Currency.java:657)
at java.util.Currency.access$100(Currency.java:76)
at java.util.Currency$1.run(Currency.java:211)
at java.security.AccessController.doPrivileged(Native Method)
at java.util.Currency.<clinit>(Currency.java:192)
at java.text.DecimalFormatSymbols.initialize(DecimalFormatSymbols.java:566)
at java.text.DecimalFormatSymbols.<init>(DecimalFormatSymbols.java:94)
at java.text.DecimalFormatSymbols.getInstance(DecimalFormatSymbols.java:157)
at java.text.NumberFormat.getInstance(NumberFormat.java:767)
at java.text.NumberFormat.getIntegerInstance(NumberFormat.java:439)
at java.text.SimpleDateFormat.initialize(SimpleDateFormat.java:664)
at java.text.SimpleDateFormat.<init>(SimpleDateFormat.java:585)
at java.text.SimpleDateFormat.<init>(SimpleDateFormat.java:560)
at Test.main(Test.java:5)
Could someone please explain this to me?
The java version we are using is:
java version "1.7.0_45"
Java(TM) SE Runtime Environment (build 1.7.0_45-b18)
Java HotSpot(TM) 64-Bit Server VM (build 24.45-b08, mixed mode)
When we are using a prior version, we have no problem:
java -version
java version "1.7.0_25"
Java(TM) SE Runtime Environment (build 1.7.0_25-b16)
Java HotSpot(TM) 64-Bit Server VM (build 23.25-b01, mixed mode)
java Test
formatted date: ma 21/10/2013 10:19
Update: Before everyone is mentioning increasing the heap size... I already tried it:
java -Xmx2048M -Xms2048M Test
Runtime.getRuntime().freeMemory(): 2048120480
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
at java.util.Currency.readLongArray(Currency.java:657)
at java.util.Currency.access$100(Currency.java:76)
at java.util.Currency$1.run(Currency.java:211)
at java.security.AccessController.doPrivileged(Native Method)
at java.util.Currency.<clinit>(Currency.java:192)
at java.text.DecimalFormatSymbols.initialize(DecimalFormatSymbols.java:566)
at java.text.DecimalFormatSymbols.<init>(DecimalFormatSymbols.java:94)
at java.text.DecimalFormatSymbols.getInstance(DecimalFormatSymbols.java:157)
at java.text.NumberFormat.getInstance(NumberFormat.java:767)
at java.text.NumberFormat.getIntegerInstance(NumberFormat.java:439)
at java.text.SimpleDateFormat.initialize(SimpleDateFormat.java:664)
at java.text.SimpleDateFormat.<init>(SimpleDateFormat.java:585)
at java.text.SimpleDateFormat.<init>(SimpleDateFormat.java:560)
at Test.main(Test.java:8)
I dug into the code of java.util.Currency. It reads in a resource file located in the Java JRE, in the static Class initializer block.
String homeDir = System.getProperty("java.home");
try {
String dataFile = homeDir + File.separator +
"lib" + File.separator + "currency.data";
DataInputStream dis = new DataInputStream(
new BufferedInputStream(
new FileInputStream(dataFile)));
if (dis.readInt() != MAGIC_NUMBER) {
throw new InternalError("Currency data is possibly corrupted");
}
formatVersion = dis.readInt();
if (formatVersion != VALID_FORMAT_VERSION) {
throw new InternalError("Currency data format is incorrect");
}
dataVersion = dis.readInt();
mainTable = readIntArray(dis, A_TO_Z * A_TO_Z);
int scCount = dis.readInt();
scCutOverTimes = readLongArray(dis, scCount);
As you can see, it reads in JRE/lib/currency.data. The fourth integer contains the scCount. This integer will be too high. I guess that that file is corrupt. Try replacing it.

Categories

Resources