java and httpclient java.lang.NoClassDefFoundError - java

I have problem.
import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
public class qq {
public static void main(String[] args) throws UnsupportedEncodingException, IOException {
HttpClient httpclient = new DefaultHttpClient();
// HttpPost httppost = new HttpPost("http://192.168.0.102/uploadtest/upload_photo");
System.out.println("aaaa");
}
}
File manifest.mf:
Manifest-version: 1.0
Main-Class: qq
OS debian 8
java version:
java version "1.8.0_111"
Java(TM) SE Runtime Environment (build 1.8.0_111-b14)
Java HotSpot(TM) 64-Bit Server VM (build 25.111-b14, mixed mode)
And:
javac -cp ./lib/httpclient-4.2.5.jar:./lib/httpcore-4.4.5.jar:./lib/httpmime-4.2.5.jar:./lib/commons-logging-1.2.jar qq.java
jar cfm test.jar manifest.mf qq.class
Its ok, no error, exit code - 0.
I try:
# java -jar test.jar
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/http/impl/client/DefaultHttpClient
at qq.main(qq.java:18)
Caused by: java.lang.ClassNotFoundException: org.apache.http.impl.client.DefaultHttpClient
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 1 more
How to fix this ?

I advise you to use Maven or Gradle because the depandances are loaded automatically.
Maven:
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.1</version>
</dependency>
Gradle:
compile group: 'org.apache.httpcomponents', name: 'httpclient', version: '4.5.1'

Related

java - Exception in thread "main" java.lang.NoClassDefFoundError: io/kubernetes/client/openapi/ApiException

I've tried to reproduce the example in k8s-java in my mini-maven project.
However, I kept getting into this error - Exception in thread "main" java.lang.NoClassDefFoundError: io/kubernetes/client/openapi/ApiException.
My maven project is borrowed from maven in 5 minutes and is only changed a few lines.
App.java
package com.mycompany.app;
import io.kubernetes.client.openapi.ApiClient;
import io.kubernetes.client.openapi.ApiException;
import io.kubernetes.client.openapi.Configuration;
import io.kubernetes.client.openapi.apis.CoreV1Api;
import io.kubernetes.client.openapi.models.V1Pod;
import io.kubernetes.client.openapi.models.V1PodList;
import io.kubernetes.client.util.Config;
import java.io.IOException;
/**
* A simple example of how to use the Java API
*
* <p>Easiest way to run this: mvn exec:java
* -Dexec.mainClass="io.kubernetes.client.examples.Example"
*
* <p>From inside $REPO_DIR/examples
*/
public class App {
public static void main(String[] args) throws IOException, ApiException {
ApiClient client = Config.defaultClient();
Configuration.setDefaultApiClient(client);
CoreV1Api api = new CoreV1Api();
V1PodList list =
api.listPodForAllNamespaces(null, null, null, null, null, null, null, null, null);
for (V1Pod item : list.getItems()) {
System.out.println(item.getMetadata().getName());
}
}
}
pom.xml
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.kubernetes</groupId>
<artifactId>client-java</artifactId>
<version>10.0.0</version>
</dependency>
</dependencies>
Commands
mvn clean package
java -cp target/my-app-1.0-SNAPSHOT.jar com.mycompany.app.App
Errors
Error: A JNI error has occurred, please check your installation and try again
Exception in thread "main" java.lang.NoClassDefFoundError: io/kubernetes/client/openapi/ApiException
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Class.java:2701)
at java.lang.Class.privateGetMethodRecursive(Class.java:3048)
at java.lang.Class.getMethod0(Class.java:3018)
at java.lang.Class.getMethod(Class.java:1784)
at sun.launcher.LauncherHelper.validateMainClass(LauncherHelper.java:650)
at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:632)
Caused by: java.lang.ClassNotFoundException: io.kubernetes.client.openapi.ApiException
at java.net.URLClassLoader.findClass(URLClassLoader.java:382)
at java.lang.ClassLoader.loadClass(ClassLoader.java:418)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:352)
at java.lang.ClassLoader.loadClass(ClassLoader.java:351)
... 7 more
Thanks anyone for the help!
run using mvn exec:java -D exec.mainClass=com.mycompany.app.App

Appium: ClassNotFoundException: org.openqa.selenium.remote.internal.ApacheHttpClient$Factory

In my maven project, i have the below dependencies:
<!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.141.59</version>
</dependency>
<dependency>
<groupId>io.appium</groupId>
<artifactId>java-client</artifactId>
<version>3.4.1</version>
</dependency>
and then, i have a very basic code just to open an app:
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import org.openqa.selenium.remote.DesiredCapabilities;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.android.AndroidElement;
import io.appium.java_client.remote.MobileCapabilityType;
File f = new File("src");
File fs = new File(f,"ApiDemos-debug.apk");
DesiredCapabilities cap = new DesiredCapabilities();
cap.setCapability(MobileCapabilityType.DEVICE_NAME, "myAVD");
cap.setCapability(MobileCapabilityType.APP, fs.getAbsolutePath());
AndroidDriver<AndroidElement> ad = new AndroidDriver<AndroidElement>(new URL("http://127.0.0.1:4723/wd/hub"),cap);
But, it complains with the below error:
Exception in thread "main" java.lang.NoClassDefFoundError: org/openqa/selenium/remote/internal/ApacheHttpClient$Factory
at io.appium.java_client.remote.AppiumCommandExecutor.<init>(AppiumCommandExecutor.java:50)
at io.appium.java_client.AppiumDriver.<init>(AppiumDriver.java:77)
at io.appium.java_client.android.AndroidDriver.<init>(AndroidDriver.java:67)
at Appium.Appium.App.main(App.java:30)
Caused by: java.lang.ClassNotFoundException: org.openqa.selenium.remote.internal.ApacheHttpClient$Factory
at java.net.URLClassLoader.findClass(URLClassLoader.java:382)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:349)
Are my dependencies wrong?
How can i fix it?
The first thing to keep in mind: don't mix selenium & appium dependencies. Appium already has selenium dependency and explicitly setting one you may cause an issue.
The other reasonable question: you are using quite an old client version, the current one is 7.3.0 Check what Appium server you got installed and if it is 1.15.x+ you probably need to use fresh client as well.

Jython is not loading the fxml file

This is the test.fxml:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.BorderPane?>
<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1">
<center>
<Label text="It is all OK" BorderPane.alignment="CENTER" />
</center>
</BorderPane>
I did a Main.java that launchs the window, like follow:
import java.io.IOException;
import java.net.URL;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.stage.Stage;
import javafx.scene.Parent;
import javafx.scene.Scene;
public class Main extends Application {
public static void main(String args[]) {
launch(args);
}
public void start(Stage stage) {
try {
URL url = this.getClass().getResource("/test.fxml");
Parent root = FXMLLoader.load(url);
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
} catch (Exception e) {
// ignore
}
}
}
It works.
But the main.py just can not launch and it is almost equal to Main.java.
I did 2 versions of this file that launchs different errors.
This is the first version:
import sys
from java.io import IOException
from java.net import URL
from javafx.application import Application
from javafx.fxml import FXMLLoader
from javafx.stage import Stage
from javafx.scene import Parent
from javafx.scene import Scene
class Main(Application):
#classmethod
def main(cls, args):
Main.launch(cls, args)
def start(self, stage):
try:
url = self.getClass().getResource('/test.fxml')
root = FXMLLoader.load(url)
scene = Scene(root)
stage.setScene(scene)
stage.show()
except Exception as exc:
pass
if __name__ == '__main__':
Main.main(sys.argv)
That shows:
Exception in Application start method
Traceback (most recent call last):
File "/home/flima/dev/db/herbalife/sell/main.py", line 48, in
Main.main(sys.argv)
File "/home/flima/dev/db/herbalife/sell/main.py", line 29, in main
Main.launch(cls, args)
at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$154(LauncherImpl.java:182)
at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.NullPointerException
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)
java.lang.RuntimeException: java.lang.RuntimeException: Exception in Application start method
This is the second version:
import sys
from java.io import IOException
from java.net import URL
from javafx.application import Application
from javafx.fxml import FXMLLoader
from javafx.stage import Stage
from javafx.scene import Parent
from javafx.scene import Scene
class Main(Application):
#classmethod
def main(cls, args):
Main.launch(cls, args)
def start(self, stage):
try:
url = self.getClass().getResource('/test.fxml')
self.loader = FXMLLoader(url) # changed here
root = self.loader.load() # changed here
scene = Scene(root)
stage.setScene(scene)
stage.show()
except Exception as exc:
pass
if __name__ == '__main__':
Main.main(sys.argv)
I get another error, that is:
Exception in Application start method
Traceback (most recent call last):
File "/home/flima/dev/db/herbalife/sell/main.py", line 48, in
Main.main(sys.argv)
File "/home/flima/dev/db/herbalife/sell/main.py", line 29, in main
Main.launch(cls, args)
at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$154(LauncherImpl.java:182)
at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.IllegalStateException: Location is not set.
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2434)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2409)
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)
java.lang.RuntimeException: java.lang.RuntimeException: Exception in Application start method
Where is my mistake?
All the files are in the same folder.
Please, help me. Thanks.
Jython version is:
Jython 2.7.0 (default:9987c746f838, Apr 29 2015, 02:25:11)
Java version is:
java version "1.8.0_191"
Java(TM) SE Runtime Environment (build 1.8.0_191-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.191-b12, mixed mode)
I am running the code at Lubuntu based on Ubuntu 18.04.1 LTS.

#WebServlet annotation doesn't work

I checked the following possible answers but not found fix for my problem
1. #WebServlet annotation with Tomcat 7
2. #WebServlet annotation not recognized; init doesn't run
3. #WebServlet annotation doesn't work with Tomcat 8
Here is my servlet-api.jar info:
Manifest-Version: 1.0
Ant-Version: Apache Ant 1.8.4
Created-By: 1.6.0_37-b06 (Sun Microsystems Inc.)
X-Compile-Source-JDK: 1.6
X-Compile-Target-JDK: 1.6
Name: javax/servlet/
Specification-Title: Java API for Servlets
Specification-Version: 3.0
Specification-Vendor: Sun Microsystems, Inc.
Implementation-Title: javax.servlet
Implementation-Version: 3.0.FR
Implementation-Vendor: Apache Software Foundation
My example class code:
import javax.servlet.annotation.WebServlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
#WebServlet("/ProcessPayment")
public class ProcessPayment extends javax.servlet.http.HttpServlet{
public void doGet(HttpServletRequest req, HttpServletResponse res)throws IOException, ServletException{
res.setContentType("text/html; charset=UTF-8");
StringWriter sWriter = new StringWriter();
PrintWriter out = new PrintWriter(sWriter);
try{
SiteTemplate st = new SiteTemplate();
PaymentDecorator pd = new PaymentDecorator();
out.print(
st.header(req)+st.headerButtons(req)+
"<tr><td>"+
pd.ccform(300,"6",1)+
"</td></tr>"+
st.footer());
}catch(Exception ee){
ErrorCheck ec = new ErrorCheck();
ec.errorMsg("ERROR: GSW.ProcessPayment.java", ee);
}
res.getWriter().print(sWriter.toString());
}
}
My web.xml:
<web-app
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
When I access it like: http://127.0.0.1:7070/ProcessPayment then following message appears:
HTTP Status 404 - /ProcessPayment
type Status report
message /ProcessPayment
description The requested resource (/ProcessPayment) is not available.
Apache Tomcat/6.0.18
I updated servlet-api but my Tomcat version in Release Note file is:
Apache Tomcat Version 6.0.18
Please advise as to why #WebServlet annotation is not working?`

Cannot compile and run Android Market API

I am sorry for the very basic question but I feel truly lost here. What I am trying to do is to compile and run this code:
package com.gc.android.market.api;
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.io.InputStream;
import com.gc.android.market.api.MarketSession;
import com.gc.android.market.api.MarketSession.Callback;
import com.gc.android.market.api.model.Market.AppType;
import com.gc.android.market.api.model.Market.AppsRequest;
import com.gc.android.market.api.model.Market.AppsRequest.OrderType;
import com.gc.android.market.api.model.Market.AppsRequest.ViewType;
import com.gc.android.market.api.model.Market.AppsResponse;
import com.gc.android.market.api.model.Market.CommentsRequest;
import com.gc.android.market.api.model.Market.GetAssetResponse.InstallAsset;
import com.gc.android.market.api.model.Market.GetImageRequest;
import com.gc.android.market.api.model.Market.GetImageResponse;
import com.gc.android.market.api.model.Market.ResponseContext;
import com.gc.android.market.api.model.Market.GetImageRequest.AppImageUsage;
public class Fetcher
{
public static void main(String[] args) {
MarketSession session = new MarketSession(true);
System.out.println("Login:");
}
}
I am following the instructions from here: https://code.google.com/p/android-market-api/wiki/HowDownloadApps
The import and package lines I got from here: https://code.google.com/p/android-market-api/source/browse/trunk/AndroidMarketApi/src/com/gc/android/market/api/Main.java
I am doing this on OS X. In my current directory I have three files:
Fetcher.java (the code above)
AndroidMarketApi.jar
protobuf-java-2.2.0.jar
I compile using this command:
$ javac -classpath ".:/Users/ephracis/fetcher/*" Fetcher.java
I then try to run:
$ java Fetcher
Exception in thread "main" java.lang.NoClassDefFoundError: Fetcher (wrong name: com/gc/android/market/api/Fetcher)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClassCond(ClassLoader.java:631)
at java.lang.ClassLoader.defineClass(ClassLoader.java:615)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:141)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:283)
at java.net.URLClassLoader.access$000(URLClassLoader.java:58)
at java.net.URLClassLoader$1.run(URLClassLoader.java:197)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
$ java com.gc.android.market.api.Fetcher
Exception in thread "main" java.lang.NoClassDefFoundError: com/gc/android/market/api/Fetcher
Caused by: java.lang.ClassNotFoundException: com.gc.android.market.api.Fetcher
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
I've also tried to add the classpath to the java command but it doesn't change anything.
To execute you need the fully qualified class name (package + class name). Try:
java -classpath ".:/Users/ephracis/fetcher/*" com.gc.android.market.api.Fetcher
Also, you still need to include the appropriate classes/jars on the classpath.

Categories