Compiler-Fehler Ein weiteres Java Problem

Olli447

Mitglied
Hallo Community,

ich versuche gerade mit Java ein Programm zu erstellen, welches bei Ausführung eine Textdatei ausließt und den Inhalt in gewissen Unterteilungen als einzelne Themen eines bestimmten Forums (phpbb3.1.X) veröffentlicht. Schnell bin ich auf dieses Thema gestoßen. Ich versuche nun das Programm aus diesem Post zu reproduzieren.
Dazu verwende ich folgenden Quelltext:

Java:
package ---.poster.org;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang.StringEscapeUtils;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.params.ClientPNames;
import org.apache.http.client.params.CookiePolicy;
import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.CoreProtocolPNames;
import org.apache.http.util.EntityUtils;

public class PhpBB3Client {

	private final String url;

	private final DefaultHttpClient httpClient;

	private final BasicCookieStore cookieStore = new BasicCookieStore();

	private static final Pattern pSID = Pattern.compile("<input\\s+type=\"hidden\"\\s+name=\"sid\"\\s+value=\"([a-zA-Z0-9]+)\"\\s+/>",
			Pattern.CASE_INSENSITIVE | Pattern.MULTILINE);

	private static final Pattern pREDIRECT = Pattern.compile("<input\\s+type=\"hidden\"\\s+name=\"redirect\"\\s+value=\"(\\S+)\"\\s+/>",
			Pattern.CASE_INSENSITIVE | Pattern.MULTILINE);

	private static final Pattern pCREATION_TIME = Pattern.compile("<input\\s+type=\"hidden\"\\s+name=\"creation_time\"\\s+value=\"(\\d+)\"\\s+/>",
			Pattern.CASE_INSENSITIVE | Pattern.MULTILINE);

	private static final Pattern pFORM_TOKEN = Pattern.compile("<input\\s+type=\"hidden\"\\s+name=\"form_token\"\\s+value=\"([a-zA-Z0-9]+)\"\\s+/>",
			Pattern.CASE_INSENSITIVE | Pattern.MULTILINE);

	private static final Pattern pPOST_ACTION=Pattern.compile("<form\\s+id=\"postform\"\\s+method=\"post\"\\s+action=\"(.+)\"\\s+enctype=\"multipart/form-data\"\\s*>",
			Pattern.CASE_INSENSITIVE | Pattern.MULTILINE);

	private static final Pattern pLAST_CLICK = Pattern.compile("<input\\s+type=\"hidden\"\\s+name=\"lastclick\"\\s+value=\"(\\d+)\"\\s+/>",
			Pattern.CASE_INSENSITIVE | Pattern.MULTILINE);

	public PhpBB3Client (final String url) {
		this.url = url+(url.endsWith("/") ? "" : "/");

		httpClient = new DefaultHttpClient();
		httpClient.setCookieStore(cookieStore);
		httpClient.getParams().setBooleanParameter(CoreProtocolPNames.USE_EXPECT_CONTINUE, false);
		httpClient.getParams().setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.BROWSER_COMPATIBILITY);
	}

	public void login(final String username, final String password) {

		try {
			final String postURL=url + "ucp.php?mode=login";
			final HttpGet get = new HttpGet(postURL);

			HttpResponse response = httpClient.execute(get);
			if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
				throw new IllegalStateException("Expected 200, got: " + response.getStatusLine().getStatusCode());
			}

			String content = EntityUtils.toString(response.getEntity());

			Matcher m = pSID.matcher(content);
			if(!m.find()) {
				throw new IllegalStateException("Could not parse SID.");
			}
			final String sid = m.group(1);

			m = pREDIRECT.matcher(content);
			if(!m.find()) {
				throw new IllegalStateException("Could not parse \"redirect\".");
			}
			final String redirect=m.group(1);

			final List<NameValuePair> formparams = new ArrayList<NameValuePair>();
			formparams.add(new BasicNameValuePair("username", username));
			formparams.add(new BasicNameValuePair("password", password));
			formparams.add(new BasicNameValuePair("autologin", "on"));
			formparams.add(new BasicNameValuePair("viewonline", "on"));
			formparams.add(new BasicNameValuePair("viewonline", "on"));
			formparams.add(new BasicNameValuePair("sid", sid));
			formparams.add(new BasicNameValuePair("login", "Login"));
			formparams.add(new BasicNameValuePair("redirect", redirect));	

			final UrlEncodedFormEntity loginPost = new UrlEncodedFormEntity(formparams, "UTF-8");
			HttpPost httpPost = new HttpPost(postURL);
			httpPost.setEntity(loginPost);

			response = httpClient.execute(httpPost);
			if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
				throw new IllegalStateException("Expected 200, got: " + response.getStatusLine().getStatusCode());
			}

			response.getEntity().consumeContent();	

		} catch (final Throwable t) {
			throw new RuntimeException("Could not login! ", t);
		}
	}

	public void newTopic(final long forumId, final String subject, final String message ) {
		try {

			final String postURL = url + "posting.php?mode=post&f=" + forumId;
			final HttpGet get = new HttpGet(postURL);

			HttpResponse response = httpClient.execute(get);
			if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
				throw new IllegalStateException("Expected 200, got: " + response.getStatusLine().getStatusCode());
			}

			String content = EntityUtils.toString(response.getEntity());

			Matcher m = pCREATION_TIME.matcher(content);
			if(!m.find()) {
				throw new IllegalStateException("Could not parse creation_time."+content);
			}
			final String creationTime = m.group(1);

			m = pFORM_TOKEN.matcher(content);
			if(!m.find()) {
				throw new IllegalStateException("Could not parse form_token.");
			}
			final String formToken=m.group(1);

			m = pLAST_CLICK.matcher(content);
			if(!m.find()) {
				throw new IllegalStateException("Could not parse last_click.");
			}
			final String lastClick=m.group(1);

			m = pPOST_ACTION.matcher(content);
			if(!m.find()) {
				throw new IllegalStateException("Could not parse post_action.");
			}
			String postAction=m.group(1);
			if(postAction.startsWith("./")) {
				postAction=postAction.substring(2, postAction.length());
			}
			postAction=StringEscapeUtils.unescapeHtml(postAction);

			final MultipartEntity mpe = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
			mpe.addPart("icon", new StringBody("0"));
			mpe.addPart("subject", new StringBody(subject));
			mpe.addPart("addbbcode20", new StringBody("100"));
			mpe.addPart("message", new StringBody(message));
			mpe.addPart("lastclick", new StringBody(""+(Integer.valueOf(lastClick)-3000)));
			mpe.addPart("post", new StringBody("Submit"));
			mpe.addPart("attach_sig", new StringBody("on"));
			mpe.addPart("topic_type", new StringBody("0"));
			mpe.addPart("topic_time_limit", new StringBody("0"));
			mpe.addPart("creation_time", new StringBody(creationTime));
			mpe.addPart("form_token", new StringBody(formToken));

			final HttpPost httpPost = new HttpPost(url+postAction);
			httpPost.setHeader("Referer", postURL);
			httpPost.setEntity(mpe);

			response = httpClient.execute(httpPost);
			response.getEntity().consumeContent();

		} catch (final Throwable t) {
			throw new RuntimeException("Could not post new Thread to forum "+forumId, t);
		}
	}

	public static void main(String[] args) {
		final PhpBB3Client client = new PhpBB3Client("XXX.de");
		client.login("XXX", "XXXX");
		client.newTopic(12, "Test", "Hello world! blablablablaba"+System.currentTimeMillis());
	}
}

die entsprechenden JAR Verzeichnisse mit den Apache Bibliotekten liegen in dem Projektordner, allerdings nicht im src Ordner, sondern eine Ebene davor.
Wenn ich das Programm nun starte schmeißt mir Java diese Fehlermeldung entgegen:

Java:
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory
	at org.apache.http.impl.client.AbstractHttpClient.<init>(AbstractHttpClient.java:182)
	at org.apache.http.impl.client.DefaultHttpClient.<init>(DefaultHttpClient.java:150)
	at ---.poster.org.PhpBB3Client.<init>(PhpBB3Client.java:55)
	at ---.poster.org.PhpBB3Client.main(PhpBB3Client.java:179)
Caused by: java.lang.ClassNotFoundException: org.apache.commons.logging.LogFactory
	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)
	... 4 more

Könnt ihr mir helfen? Eventuell hab ihr ja auch Anregungen wie ihr so etwas umsetzen würdet ;-)

LG

Oliver
 
Zuletzt bearbeitet:
Ja dem Fehler habe ich bereits "hinterher" gegoogelt, nur macht das für mich keinen Sinn :bahnhof: . Das würde dann doch bedeuten, dass die offizelle "Quell-jar" fehlerhaft ist ???:L . Ich kann doch schlecht die benötigte Klasse selber schreiben, oder?
Auch verstehe ich nicht so ganz warum der Compiler versucht diese Klasse zu laden. Ich kann nichts entdecken, was das logging auslösen würde, oder habe ich eine Befehlzeile übersehen (sprich bin zu blind)?

Warscheinlich sehe ich mal wieder den Wald vor lauter Bäumen nicht 🙂

Grüße
Oliver
 
Ja dem Fehler habe ich bereits "hinterher" gegoogelt, nur macht das für mich keinen Sinn :bahnhof: . Das würde dann doch bedeuten, dass die offizelle "Quell-jar" fehlerhaft ist ???:L . Ich kann doch schlecht die benötigte Klasse selber schreiben, oder?

Nein, aber diese "Quell-Jar" kann weiterer JARs benötigen, welche dir vielleicht fehlen 🙂
Sprich du solltest kontrollieren welche Abhängigkeiten dieses JAR hat, welche dir fehlen.

Auch verstehe ich nicht so ganz warum der Compiler versucht diese Klasse zu laden. Ich kann nichts entdecken, was das logging auslösen würde, oder habe ich eine Befehlzeile übersehen (sprich bin zu blind)?

Code:
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory
at org.apache.http.impl.client.AbstractHttpClient.<init>(AbstractHttpClient.java:182)
at org.apache.http.impl.client.DefaultHttpClient.<init>(DefaultHttpClient.java:150)
at ---.poster.org.PhpBB3Client.<init>(PhpBB3Client.java:55)
at ---.poster.org.PhpBB3Client.main(PhpBB3Client.java:179)

Da steht doch ein StackTrace der sagt dir wo der Fehler passiert.
Du selbst rufst in deinem Code das Logging vielleicht nicht auf, aber die Klasse "PhpBB3Client" macht dies, bzw. basiert auf Code der dieses Logging aufruft!
 
Zuletzt bearbeitet:

Zurück
Oben