in java catch can't catch exception with jsoup - java

I want to parse a website with jsoup, and it succeeded to get the detail what I want, but I still get many many exceptions and not in catch
public static void main(String[] argv)
{
getGoogle();
}
private static void getGoogle() {
String url = "http://google.com";
Document doc = null;
try {
doc = Jsoup.connect(url).get();
Element body = doc.body();
}catch (Exception e) {
System.out.println("------------google------------");
System.out.println("getGoogle:" + e.getMessage());
}
}
when I use debug mode found it was because doc = Jsoup.connect(url).get(); this line, but it looks ok after I search much info, below is exceptions I got
Could not instrument class com/sun/deploy/security/MozillaJSSProvider: java.lang.IllegalArgumentException
at org.eclipse.jdt.launching.internal.org.objectweb.asm.ClassReader.<init>(Unknown Source)
at org.eclipse.jdt.launching.internal.weaving.ClassfileTransformer.transform(ClassfileTransformer.java:25)
at org.eclipse.jdt.launching.internal.javaagent.Premain$1.transform(Premain.java:51)
at java.instrument/java.lang.instrument.ClassFileTransformer.transform(ClassFileTransformer.java:246)
at java.instrument/sun.instrument.TransformerManager.transform(TransformerManager.java:188)
at java.instrument/sun.instrument.InstrumentationImpl.transform(InstrumentationImpl.java:550)
at java.base/java.lang.ClassLoader.defineClass2(Native Method)
at java.base/java.lang.ClassLoader.defineClass(ClassLoader.java:1099)
at java.base/java.security.SecureClassLoader.defineClass(SecureClassLoader.java:206)
at java.base/jdk.internal.loader.BuiltinClassLoader.defineClass(BuiltinClassLoader.java:760)
at java.base/jdk.internal.loader.BuiltinClassLoader.findClassInModuleOrNull(BuiltinClassLoader.java:681)
at java.base/jdk.internal.loader.BuiltinClassLoader.findClass(BuiltinClassLoader.java:562)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:608)
at java.base/java.lang.Class.forName(Class.java:448)
at java.base/java.util.ServiceLoader.loadProvider(ServiceLoader.java:847)
at java.base/java.util.ServiceLoader.access$600(ServiceLoader.java:390)
at java.base/java.util.ServiceLoader$ModuleServicesLookupIterator.hasNext(ServiceLoader.java:1071)
at java.base/java.util.ServiceLoader$2.hasNext(ServiceLoader.java:1294)
at java.base/java.util.ServiceLoader$3.hasNext(ServiceLoader.java:1379)
at java.base/sun.security.jca.ProviderConfig$ProviderLoader.load(ProviderConfig.java:334)
at java.base/sun.security.jca.ProviderConfig$3.run(ProviderConfig.java:244)
at java.base/sun.security.jca.ProviderConfig$3.run(ProviderConfig.java:238)
at java.base/java.security.AccessController.doPrivileged(Native Method)
at java.base/sun.security.jca.ProviderConfig.doLoadProvider(ProviderConfig.java:238)
at java.base/sun.security.jca.ProviderConfig.getProvider(ProviderConfig.java:218)
at java.base/sun.security.jca.ProviderList.getProvider(ProviderList.java:266)
at java.base/sun.security.jca.ProviderList.getIndex(ProviderList.java:296)
at java.base/sun.security.jca.ProviderList.getProviderConfig(ProviderList.java:280)
at java.base/sun.security.jca.ProviderList.getProvider(ProviderList.java:286)
at java.base/java.security.Security.getProvider(Security.java:470)
at java.base/sun.security.ssl.SignatureAndHashAlgorithm.<clinit>(SignatureAndHashAlgorithm.java:415)
at java.base/sun.security.ssl.SSLSessionImpl.<init>(SSLSessionImpl.java:182)
at java.base/sun.security.ssl.SSLSessionImpl.<init>(SSLSessionImpl.java:150)
at java.base/sun.security.ssl.SSLSessionImpl.<clinit>(SSLSessionImpl.java:79)
at java.base/sun.security.ssl.SSLSocketImpl.init(SSLSocketImpl.java:596)
at java.base/sun.security.ssl.SSLSocketImpl.<init>(SSLSocketImpl.java:534)
at java.base/sun.security.ssl.SSLSocketFactoryImpl.createSocket(SSLSocketFactoryImpl.java:72)
at java.base/sun.net.www.protocol.https.HttpsClient.createSocket(HttpsClient.java:413)
at java.base/sun.net.NetworkClient.doConnect(NetworkClient.java:162)
at java.base/sun.net.www.http.HttpClient.openServer(HttpClient.java:474)
at java.base/sun.net.www.http.HttpClient.openServer(HttpClient.java:569)
at java.base/sun.net.www.protocol.https.HttpsClient.<init>(HttpsClient.java:265)
at java.base/sun.net.www.protocol.https.HttpsClient.New(HttpsClient.java:372)
at java.base/sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.getNewHttpClient(AbstractDelegateHttpsURLConnection.java:191)
at java.base/sun.net.www.protocol.http.HttpURLConnection.plainConnect0(HttpURLConnection.java:1181)
at java.base/sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:1075)
at java.base/sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(AbstractDelegateHttpsURLConnection.java:177)
at java.base/sun.net.www.protocol.https.HttpsURLConnectionImpl.connect(HttpsURLConnectionImpl.java:163)
at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:651)
at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:676)
at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:676)
at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:628)
at org.jsoup.helper.HttpConnection.execute(HttpConnection.java:260)
at org.jsoup.helper.HttpConnection.get(HttpConnection.java:249)
at test3.test3.getGoogle(test3.java:23)
at test3.test3.main(test3.java:12)
Could not instrument class com/sun/deploy/security/MozillaJSSProvider$ProviderService: java.lang.IllegalArgumentException
at org.eclipse.jdt.launching.internal.org.objectweb.asm.ClassReader.<init>(Unknown Source)
at org.eclipse.jdt.launching.internal.weaving.ClassfileTransformer.transform(ClassfileTransformer.java:25)
Could not instrument class com/sun/deploy/security/MozillaJSSProvider$ProviderService: java.lang.IllegalArgumentException
at org.eclipse.jdt.launching.internal.org.objectweb.asm.ClassReader.<init>(Unknown Source)
at org.eclipse.jdt.launching.internal.weaving.ClassfileTransformer.transform(ClassfileTransformer.java:25)
Could not instrument class sun/security/pkcs11/SunPKCS11: java.lang.IllegalArgumentException
at org.eclipse.jdt.launching.internal.org.objectweb.asm.ClassReader.<init>(Unknown Source)
at org.eclipse.jdt.launching.internal.weaving.ClassfileTransformer.transform(ClassfileTransformer.java:25)
Could not instrument class sun/security/pkcs11/wrapper/PKCS11Exception: java.lang.IllegalArgumentException
at org.eclipse.jdt.launching.internal.org.objectweb.asm.ClassReader.<init>(Unknown Source)
at org.eclipse.jdt.launching.internal.weaving.ClassfileTransformer.transform(ClassfileTransformer.java:25)
Could not instrument class sun/security/pkcs11/SunPKCS11$Descriptor: java.lang.IllegalArgumentException
at org.eclipse.jdt.launching.internal.org.objectweb.asm.ClassReader.<init>(Unknown Source)
at org.eclipse.jdt.launching.internal.weaving.ClassfileTransformer.transform(ClassfileTransformer.java:25)
Could not instrument class com/sun/security/sasl/gsskerb/JdkSASL: java.lang.IllegalArgumentException
at org.eclipse.jdt.launching.internal.org.objectweb.asm.ClassReader.<init>(Unknown Source)
at org.eclipse.jdt.launching.internal.weaving.ClassfileTransformer.transform(ClassfileTransformer.java:25)
at org.eclipse.jdt.launching.internal.javaagent.Premain$1.transform(Premain.java:51)
Could not instrument class com/sun/security/sasl/gsskerb/JdkSASL$1: java.lang.IllegalArgumentException
at org.eclipse.jdt.launching.internal.org.objectweb.asm.ClassReader.<init>(Unknown Source)
at org.eclipse.jdt.launching.internal.weaving.ClassfileTransformer.transform(ClassfileTransformer.java:25)
at org.eclipse.jdt.launching.internal.javaagent.Premain$1.transform(Premain.java:51)
and more more exceptions.....and all not catch by try...catch

It is an eclipse bug, you can find the discussion here
Also, this has been raised in SO as well, this (https://stackoverflow.com/a/47109099/6039974) answer might help you.

Related

java.io.IOException: Stream closed when unmarshaling with JAXBContext

I wrote an unmarshal method which is accessed by a bunch of tests running in parallel to read data from XML files.
This method sits in a maven commons dependency module added to my main module.
I am facing java.io.IOException: Stream closed exception in a very inconsistent fashion and cannot reproduce it locally. It only happens when my tests run in CI in Jenkins and that too not very often. Code is below.
public <T> Object internalUnmarshal(String url, Class<T> clazz)
throws JAXBException,DataException, ClassNotFoundException,IOException
{
InputStream is = getClass().getResourceAsStream(url);
try {
if (is == null) {
throw new DataException("''{0}'' not found.", url);
}
JAXBContext jaxbContext = JAXBContext
.newInstance(Class.forName(clazz.getCanonicalName());
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
return unmarshaller.unmarshal(is);
} catch (JAXBException e) {
throw new JAXBException(e);
} finally {
is.close();
}
}
I even added a synchronized block as below but same issue was reproduced in jenkins server run.
synchronized(this){
return unmarshaller.unmarshal(is);
}
Stack trace below :-
[javax.xml.bind.UnmarshalException
- with linked exception:
[java.io.IOException: Stream closed]]
at com.paydiant.commons.data.unmarshalers.XMLUnmarshaler.internalUnmarshal(XMLUnmarshaler.java:116)
at com.paydiant.commons.data.unmarshalers.XMLUnmarshaler.resloveToUnmarshaledObjectList(XMLUnmarshaler.java:76)
at com.paydiant.commons.data.unmarshalers.XMLUnmarshaler.doUnmarshalObjects(XMLUnmarshaler.java:36)
... 67 more
Caused by: javax.xml.bind.UnmarshalException
- with linked exception:
[java.io.IOException: Stream closed]
at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:261)
at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(UnmarshallerImpl.java:229)
at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:157)
at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:204)
at com.paydiant.commons.data.unmarshalers.XMLUnmarshaler.internalUnmarshal(XMLUnmarshaler.java:112)
... 69 more
Caused by: java.io.IOException: Stream closed
at java.util.zip.InflaterInputStream.ensureOpen(InflaterInputStream.java:67)
at java.util.zip.InflaterInputStream.read(InflaterInputStream.java:121)
at java.io.FilterInputStream.read(FilterInputStream.java:83)
at java.io.FilterInputStream.read(FilterInputStream.java:83)
at org.apache.xerces.impl.XMLEntityManager$RewindableInputStream.read(Unknown Source)
at org.apache.xerces.impl.XMLEntityManager.setupCurrentEntity(Unknown Source)
at org.apache.xerces.impl.XMLVersionDetector.determineDocVersion(Unknown Source)
at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
at org.apache.xerces.parsers.AbstractSAXParser.parse(Unknown Source)
at org.apache.xerces.jaxp.SAXParserImpl$JAXPSAXParser.parse(Unknown Source)
at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:258)
... 73 more
Any ideas what might be causing this ?

IOException Loading Data into BlazegraphEmbedded

I'm having an issue loading my Blazegraph properties file into an embedded instance. When I try to import my .properties file into my Java class, I get the following error:
Exception in thread "main" java.io.IOException: Stream closed
at java.io.BufferedInputStream.getInIfOpen(Unknown Source)
at java.io.BufferedInputStream.read1(Unknown Source)
at java.io.BufferedInputStream.read(Unknown Source)
at sun.nio.cs.StreamDecoder.readBytes(Unknown Source)
at sun.nio.cs.StreamDecoder.implRead(Unknown Source)
at sun.nio.cs.StreamDecoder.read(Unknown Source)
at java.io.InputStreamReader.read(Unknown Source)
at java.io.Reader.read(Unknown Source)
at java.util.Properties$LineReader.readLine(Unknown Source)
at java.util.Properties.load0(Unknown Source)
at java.util.Properties.load(Unknown Source)
at blazegraph_tinkerpop_tryout.blazegraph_data_load.loadProperties(blazegraph_data_load.java:55)
at blazegraph_tinkerpop_tryout.blazegraph_data_load.main(blazegraph_data_load.java:32)
Call to loadProperties function from main:
Properties props = loadProperties("sampleprops.properties");
My loadProperties function (checking to see whether file path is valid, then sending to reader):
public static Properties loadProperties(String resource) throws IOException
{
Properties p = new Properties();
Path path = Paths.get(resource);
Boolean bool = Files.exists(path);
if (bool)
{
System.out.println("File was found. Attempting data load...");
InputStream is = blazegraph_data_load.class.getResourceAsStream(resource);
p.load(new InputStreamReader(new BufferedInputStream(is)));
return p;
}
System.out.println("The file you entered was not found.");
return null;
}
Here is what my file sampleprops.properties looks like:
com.bigdata.journal.AbstractJournal.bufferMode=DiskRW
com.bigdata.journal.AbstractJournal.file=blazegraph.jnl
I have been following the setup instructions from the sample Blazegraph app described here. If it makes a difference, I am using the Blazegraph/Tinkerpop3 implementation found here.
I found a workaround: I switched my getResourceAsStream method to a FileInputStream method.
The problem was with the placement of my properties file. The FileInputStream method seems more forgiving in where you place the file.

javax.tools.JavaCompiler Setting up classpath

Im trying to write scripting system for a Minecraft Spigot plugin and im not quite sure on how to give my scripts the information from my main jar file.
With this current code it will compile the script fine but give me an error on line "3" of the Compass.java file (The class declaration). From what I see the program doesnt know what "com.deft.core.scripts.DeftScript" is and im not quite sure on how to tell it this information.
I got my compiling code from this thread if its relevant
Compiling the Script:
DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<JavaFileObject>();
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
StandardJavaFileManager fileManager = compiler.getStandardFileManager(diagnostics, null, null);
List<String> optionList = new ArrayList<String>();
optionList.add("-classpath");
optionList.add(System.getProperty("java.class.path") + ";dist/Deft-Core.jar");
Iterable<? extends JavaFileObject> compilationUnit = fileManager.getJavaFileObjectsFromFiles(Arrays.asList(script));
JavaCompiler.CompilationTask task = compiler.getTask(null, fileManager, diagnostics, optionList, null, compilationUnit);
if (task.call()) {
Object obj = null;
try {
URLClassLoader classLoader = new URLClassLoader(new URL[]{new File("./plugins/Deft-Core/").toURI().toURL()});
Class<?> loadedClass;
loadedClass = classLoader.loadClass("scripts.Compass");
obj = loadedClass.newInstance();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (MalformedURLException e) {
e.printStackTrace();
}
if (obj instanceof DeftScript) {
DeftScript deftScript = (DeftScript)obj;
deftScript.onEnable(this);
}
} else {
for (Diagnostic<? extends JavaFileObject> diagnostic : diagnostics.getDiagnostics()) {
System.out.format("Error on line %d in %s%n", diagnostic.getLineNumber(), diagnostic.getSource().toUri());
}
}
Compass.java
package scripts;
public class Compass extends com.deft.core.scripts.DeftScript {
#Override
public void onEnabled() {
}
}
DeftScript.java
package com.deft.core.scripts;
public abstract class DeftScript {
public abstract void onEnable();
}
With com.deft.core.scripts.DeftScript being the source im trying to extend from the main jarfile.
Im pretty sure my problem lies from somewhere within here:
List<String> optionList = new ArrayList<String>();
optionList.add("-classpath");
optionList.add(System.getProperty("java.class.path") + ";dist/Deft-Core.jar");
EDIT:
Fixed my problem with setting up the class path. My jar file in question was location in another location from what I specified.
I switch out the optionList.add calls with one
optionList.addAll(Arrays.asList("-classpath", System.getProperty("java.class.path") + ";./plugins/Deft-Core.jar"));
All tho this fixed my initial question now it as started causing a new error.
Apon trying to run my newly compiled class file it is giving me an error
java.lang.NoClassDefFoundError: com/deft/core/scripts/DeftScript
at java.lang.ClassLoader.defineClass1(Native Method) ~[?:1.8.0_45]
at java.lang.ClassLoader.defineClass(Unknown Source) ~[?:1.8.0_45]
at java.security.SecureClassLoader.defineClass(Unknown Source) ~[?:1.8.0_45]
at java.net.URLClassLoader.defineClass(Unknown Source) ~[?:1.8.0_45]
at java.net.URLClassLoader.access$100(Unknown Source) ~[?:1.8.0_45]
at java.net.URLClassLoader$1.run(Unknown Source) ~[?:1.8.0_45]
at java.net.URLClassLoader$1.run(Unknown Source) ~[?:1.8.0_45]
at java.security.AccessController.doPrivileged(Native Method) ~[?:1.8.0_45]
at java.net.URLClassLoader.findClass(Unknown Source) ~[?:1.8.0_45]
at java.lang.ClassLoader.loadClass(Unknown Source) ~[?:1.8.0_45]
at java.lang.ClassLoader.loadClass(Unknown Source) ~[?:1.8.0_45]
at com.deft.core.main.DeftCore.onEnable(DeftCore.java:79) ~[?:?]

how to run java file in window powershell

I know there are similar questions out there but none of those solutions work for my case.
I tried to execute a Java file using Windows PowerShell.
This is what I have for Java file:
public class Demo1 {
public static void main (String arg[]){
System.out.println("DEMO1 START RUNNING");
try {
Thread.sleep(4000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("Unable to process");
}
System.out.println("DEMO1 JAVA COMPLETED.");
}
}
For easy compilation, I have copies of Demo1.java, Demo1.jar, and Demo1.class in the desktop directory.
for powershell command, I have tried:
1st attempt:
C:..\Desktop> java Demo1.java
2nd attempt:
C:..\Desktop> java -class Demo1.class
3rd attempt:
C:..\Desktop> java -jar Demo1.jar
All the above attempt return this error:
PS C:\Users\b003485\Desktop> java .\Demo1.java
Exception in thread "main" java.lang.NoClassDefFoundError: /\Demo1/java
Caused by: java.lang.ClassNotFoundException: .\Demo1.java
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
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)
Could not find the main class: .\Demo1.java. Program will exit.

Exception java.lang.NoClassDefFoundError android/app/Activity

In an android app, having:
public class Pars extends Activity{
public Document docout(){
InputStream is = getResources().openRawResource(R.raw.talechap01);
Document docout = null;
DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = null;
try {
builder = domFactory.newDocumentBuilder();
docout = builder.parse(is);
} catch (ParserConfigurationException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return docout;
}
and
public class Manager {
public static String getStory(String spage, Document docs) {
XPathExpression expr;
XPath xpath = XPathFactory.newInstance().newXPath();
Object result = null;
String num = spage;
String par = null;
try {
expr = xpath.compile("//decision"+num+"/p//text()");
result = expr.evaluate(docs, XPathConstants.NODESET);
}
catch (XPathExpressionException e) {
e.printStackTrace();
}
NodeList nodes = (NodeList) result;
for (int i = 0; i < nodes.getLength(); i++) {
par = nodes.item(i).getNodeValue();
System.out.println(par);
}
return par;
}
}
Now I want to test the code.
public class Test {
public static void main(String[] args) {
try {Pars p = new Pars();
Document doc = p.docout();
System.out.println(Manager.getStory("000", doc));
}catch (Exception e){
e.printStackTrace();
}
}
}
I run "Test" as a java application and I get:
Exception in thread "main" java.lang.NoClassDefFoundError:
android/app/Activity at java.lang.ClassLoader.defineClass1(Native
Method) at java.lang.ClassLoader.defineClass(Unknown Source) at
java.security.SecureClassLoader.defineClass(Unknown Source) at
java.net.URLClassLoader.defineClass(Unknown Source) at
java.net.URLClassLoader.access$100(Unknown Source) at
java.net.URLClassLoader$1.run(Unknown Source) at
java.net.URLClassLoader$1.run(Unknown Source) at
java.security.AccessController.doPrivileged(Native Method) 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) at
com.stack.over.Test.main(Test.java:11) Caused by:
java.lang.ClassNotFoundException: android.app.Activity at
java.net.URLClassLoader$1.run(Unknown Source) at
java.net.URLClassLoader$1.run(Unknown Source) at
java.security.AccessController.doPrivileged(Native Method) 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
I tried to find how to fix it but cannot achieve it.
I have this answer from another question but I'm kind of noob yet and can't figure it out how to fix it.
You have defined your Pars class as extending the class Activity. The
error you are seeing is telling you that the class
android.app.Activity is not available on the classpath. Maybe you need
to check which libraries you import. – adamretter Mar 5 at 10:01
Thanks all.
The error is that you're trying to run a program that references Android classes (android.app.Activity) as a regular Java program that doesn't have Android runtime libraries in its classpath.
You should run Android apps as Android apps (on an emulator or a device) and regular Java programs as regular Java programs and not mix the two. Activity is Android; main() method is regular Java.
To learn Android app development, you can start with the developer.android.com getting started documentation.
The error message says it all. You are writing Android now. Trying to write a main method doesn't make any more sense in Android than it would if you were writing a webapp that runs in Tomcat. You are going to need to step back and read up on how Android works.

Categories

Resources