Third-party jar (Twitter4J) in Minecraft Server with Bukkit - java

I have seen many errors with Twitter4j with Android, but I am not using it for Android. I am using it for Bukkit (Minecraft Plugin). For some reason, when I add twitter4j-core-3.0.3.jar to my project, I get a error when loading in the server console:
[SEVERE] Could not load 'plugins\Test.jar' in folder 'plugins'
org.bukkit.plugin.InvalidPluginException: java.lang.NoClassDefFoundError: twitter4j/TwitterException
at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.java:184)
at org.bukkit.plugin.SimplePluginManager.loadPlugin(SimplePluginManager.java:305)
at org.bukkit.plugin.SimplePluginManager.loadPlugins(SimplePluginManager.java:230)
at org.bukkit.craftbukkit.v1_6_R2.CraftServer.loadPlugins(CraftServer.java:239)
at org.bukkit.craftbukkit.v1_6_R2.CraftServer.<init>(CraftServer.java:217)
at net.minecraft.server.v1_6_R2.PlayerList.<init>(PlayerList.java:56)
at net.minecraft.server.v1_6_R2.DedicatedPlayerList.<init>(SourceFile:11)
at net.minecraft.server.v1_6_R2.DedicatedServer.init(DedicatedServer.java:106)
at net.minecraft.server.v1_6_R2.MinecraftServer.run(MinecraftServer.java:391)
at net.minecraft.server.v1_6_R2.ThreadServerApplication.run(SourceFile:582)
Caused by: java.lang.NoClassDefFoundError: twitter4j/TwitterException
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.java:173)
... 9 more
Caused by: java.lang.ClassNotFoundException: twitter4j.TwitterException
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 org.bukkit.plugin.java.PluginClassLoader.findClass0(PluginClassLoader.java:80)
at org.bukkit.plugin.java.PluginClassLoader.findClass(PluginClassLoader.java:53)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 12 more`
Since I am new to Java, I am really not getting what I am doing wrong here. This is my code (I do not think this is the issue, since the plugin fails to load ... :
String TWITTER_CONSUMER_KEY = "XXXXXXXX";
String TWITTER_SECRET_KEY = "XXXXXX";
String TWITTER_ACCESS_TOKEN = "XXXXXXXXXX";
String TWITTER_ACCESS_TOKEN_SECRET = "XXXXXX";
ConfigurationBuilder cb = new ConfigurationBuilder();
cb.setDebugEnabled(true).setOAuthConsumerKey(TWITTER_CONSUMER_KEY).setOAuthConsumerSecret(TWITTER_SECRET_KEY).setOAuthAccessToken(TWITTER_ACCESS_TOKEN).setOAuthAccessTokenSecret(TWITTER_ACCESS_TOKEN_SECRET);
TwitterFactory tf = new TwitterFactory(cb.build());
Twitter twitter = tf.getInstance();
try {
Query query = new Query(Username);
QueryResult result;
do {
result = twitter.search(query);
List<Status> tweets = result.getTweets();
for (Status tweet : tweets)
{
String rawJSON = DataObjectFactory.getRawJSON(tweet);
try
{
BufferedWriter out = new BufferedWriter(new FileWriter((getDataFolder() + File.separator + "TwitterData.txt")));
out.write(rawJSON);
out.close();
}
catch (IOException ioe)
{
ioe.printStackTrace();
System.out.println("Failed to store tweets: " + ioe.getMessage());
}
}
}
while ((query = result.nextQuery()) != null);
System.exit(0);
}
catch (TwitterException te)
{
te.printStackTrace();
System.out.println("Failed to search tweets: " + te.getMessage());
System.exit(-1);
}
Why is Twitter4j not loading properly? Thanks!
EDIT: Also, the twitter4j-core-3.03.jar is in another directory on my computer. Is that a issue?

All jars needed by your Java program need to be on the classpath on the server. This includes Twitter4J in your case.

Related

How to read data from smart card using jmrtd and scuba

I'm trying to read data from ePassporta using jmrtd and scuba.
But it doesn’t work. In Android, almost the same code works.
And when reading from a smart card it doesn’t work.
My method for reading:
public static void reader() throws Exception {
try {
CardTerminal terminal =TerminalFactory.getDefault().terminals().list().get(0);
CardService cs = CardService.getInstance(terminal);
PassportService service = new PassportService(cs);
service.open();
BACKeySpec bacKey = new BACKey("12312312312312", "31121990", "311230");
boolean paceSucceeded = false;
try {
CardAccessFile cardAccessFile = new CardAccessFile(service.getInputStream(PassportService.EF_CARD_ACCESS));
Collection<PACEInfo> paceInfos = cardAccessFile.getPACEInfos();
if (paceInfos != null && paceInfos.size() > 0) {
PACEInfo paceInfo = paceInfos.iterator().next();
service.doPACE(bacKey, paceInfo.getObjectIdentifier(), PACEInfo.toParameterSpec(paceInfo.getParameterId()));
paceSucceeded = true;
} else {
paceSucceeded = true;
}
} catch (Exception e) {
e.printStackTrace();
}
service.sendSelectApplet(paceSucceeded);
if (!paceSucceeded) {
try {
service.getInputStream(PassportService.EF_COM).read();
} catch (Exception e) {
service.doBAC(bacKey);
}
}
LDS lds = new LDS();
CardFileInputStream dg1In = service.getInputStream(PassportService.EF_DG1);
lds.add(PassportService.EF_DG1, dg1In, dg1In.getLength());
DG1File dg1File = lds.getDG1File();
System.out.println(dg1File.getMRZInfo().toString());
} catch (CardServiceException e) {
e.printStackTrace();
}
}
pom.xml
<dependency>
<groupId>org.jmrtd</groupId>
<artifactId>jmrtd</artifactId>
<version>0.5.2</version>
</dependency>
<dependency>
<groupId>net.sf.scuba</groupId>
<artifactId>scuba-sc-j2se</artifactId>
<version>0.0.13</version>
</dependency>
<dependency>
<groupId>net.sf.scuba</groupId>
<artifactId>scuba-smartcards</artifactId>
<version>0.0.13</version>
</dependency>
Stacktrace :
add more text just for stack overflow to accept the question.
added full stacktrace content.
added full stacktrace content.
added full stacktrace content.
added full stacktrace content.
D:\Java\jdk\bin\java -Didea.launcher.port=7539 "-Didea.launcher.bin.path=C:\Program Files (x86)\JetBrains\IntelliJ IDEA 14.0\bin" -Dfile.encoding=UTF-8 -classpath "D:\Java\jdk\jre\lib\charsets.jar;D:\Java\jdk\jre\lib\deploy.jar;D:\Java\jdk\jre\lib\javaws.jar;D:\Java\jdk\jre\lib\jce.jar;D:\Java\jdk\jre\lib\jfr.jar;D:\Java\jdk\jre\lib\jfxswt.jar;D:\Java\jdk\jre\lib\jsse.jar;D:\Java\jdk\jre\lib\management-agent.jar;D:\Java\jdk\jre\lib\plugin.jar;D:\Java\jdk\jre\lib\resources.jar;D:\Java\jdk\jre\lib\rt.jar;D:\Java\jdk\jre\lib\ext\access-bridge-32.jar;D:\Java\jdk\jre\lib\ext\cldrdata.jar;D:\Java\jdk\jre\lib\ext\dnsns.jar;D:\Java\jdk\jre\lib\ext\jaccess.jar;D:\Java\jdk\jre\lib\ext\jfxrt.jar;D:\Java\jdk\jre\lib\ext\localedata.jar;D:\Java\jdk\jre\lib\ext\nashorn.jar;D:\Java\jdk\jre\lib\ext\sunec.jar;D:\Java\jdk\jre\lib\ext\sunjce_provider.jar;D:\Java\jdk\jre\lib\ext\sunmscapi.jar;D:\Java\jdk\jre\lib\ext\sunpkcs11.jar;D:\Java\jdk\jre\lib\ext\zipfs.jar;C:\Users\maksat\IdeaProjects\smart_card\target\classes;C:\Users\maksat\.m2\repository\org\jmrtd\jmrtd\0.5.2\jmrtd-0.5.2.jar;C:\Users\maksat\.m2\repository\org\bouncycastle\bcprov-jdk15on\1.52\bcprov-jdk15on-1.52.jar;C:\Users\maksat\.m2\repository\net\sf\scuba\scuba-sc-j2se\0.0.11\scuba-sc-j2se-0.0.11.jar;C:\Users\maksat\.m2\repository\net\sf\scuba\scuba-smartcards\0.0.9\scuba-smartcards-0.0.9.jar;C:\Program Files (x86)\JetBrains\IntelliJ IDEA 14.0\lib\idea_rt.jar" com.intellij.rt.execution.application.AppMain paket.ReadFromSmartCard
net.sf.scuba.smartcards.CardServiceException: File not found, CAPDU = 00A4020C02011C, RAPDU = 6A82 (SW = 0x6A82: FILE NOT FOUND)
at org.jmrtd.PassportApduService.checkStatusWordAfterFileOperation(Unknown Source)
at org.jmrtd.PassportApduService.sendSelectFile(Unknown Source)
at org.jmrtd.PassportService.sendSelectFile(Unknown Source)
at org.jmrtd.MRTDFileSystem.getFileInfo(Unknown Source)
at org.jmrtd.MRTDFileSystem.getSelectedPath(Unknown Source)
at net.sf.scuba.smartcards.CardFileInputStream.<init>(CardFileInputStream.java:56)
at org.jmrtd.PassportService.getInputStream(Unknown Source)
at paket.ReadFromSmartCard.sdvsdv(ReadFromSmartCard.java:121)
at paket.ReadFromSmartCard.readData(ReadFromSmartCard.java:89)
at paket.ReadFromSmartCard.main(ReadFromSmartCard.java:23)
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)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)
ноя 19, 2019 10:45:51 AM org.jmrtd.PassportService doBAC
WARNING: BAC failed for BAC key "21009199301195, 100993, 120229"
net.sf.scuba.smartcards.CardServiceException: Mutual authentication failed: expected length: 40 + 2, actual length: 2 (SW = 0x6985: CONDITIONS NOT SATISFIED)
at org.jmrtd.PassportApduService.sendMutualAuth(Unknown Source)
at org.jmrtd.PassportService.doBAC(Unknown Source)
at org.jmrtd.PassportService.doBAC(Unknown Source)
at paket.ReadFromSmartCard.sdvsdv(ReadFromSmartCard.java:140)
at paket.ReadFromSmartCard.readData(ReadFromSmartCard.java:89)
at paket.ReadFromSmartCard.main(ReadFromSmartCard.java:23)
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)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)
Process finished with exit code 0

Trying to connect mySQL database with a java app keep getting stuck [duplicate]

This question already has answers here:
Connect Java to a MySQL database
(14 answers)
Closed 5 years ago.
ok so i have a java fx app that will display info from a mysql database runing locally on the same computer. i keep getting driver not found. here is the code:
String databaseURL = "jdbc:mysql://localhost:3307/mysql?zeroDateTimeBehavior=convertToNull";
String user = "user";
String password = "password";
Connection conn = null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(databaseURL, user, password);
if (conn != null) {
System.out.println("Connected to the database");
}
} catch (ClassNotFoundException ex) {
System.out.println("Could not find database driver class");
ex.printStackTrace();
} catch (SQLException ex) {
System.out.println("An error occurred. Maybe user/password is invalid");
ex.printStackTrace();
} finally {
if (conn != null) {
try {
conn.close();
} catch (SQLException ex) {
ex.printStackTrace();
}
}
and here is the stack trace:
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
at java.net.URLClassLoader$1.run(URLClassLoader.java:372)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:360)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:259)
at got_map.GOT_Map.start(GOT_Map.java:130)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$153(LauncherImpl.java:821)
at com.sun.javafx.application.LauncherImpl$$Lambda$51/1468396900.run(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$166(PlatformImpl.java:323)
at com.sun.javafx.application.PlatformImpl$$Lambda$44/584634336.run(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$null$164(PlatformImpl.java:292)
at com.sun.javafx.application.PlatformImpl$$Lambda$47/1960854798.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$165(PlatformImpl.java:291)
at com.sun.javafx.application.PlatformImpl$$Lambda$45/501263526.run(Unknown Source)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$141(WinApplication.java:102)
at com.sun.glass.ui.win.WinApplication$$Lambda$37/96639997.run(Unknown Source)
at java.lang.Thread.run(Thread.java:745)
Apr 21, 2017 6:29:06 PM javafx.fxml.FXMLLoader$ValueElement processValue
WARNING: Loading FXML document with JavaFX API of version 8.0.111 by JavaFX runtime of version 8.0.20
Any help would be much appreciated
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
...
at java.lang.Class.forName(Class.java:259)
at got_map.GOT_Map.start(GOT_Map.java:130)
This : Class.forName("com.mysql.jdbc.Driver"); is not enough.
Indeed, it rises a ClassNotFoundException if the class cannot be retrieved from the runtime classpath.
You need just to add in the runtime classpath the MySQL JDBC driver matching with the installed MySQL version.
The problem is in the line, Class.forName("com.mysql.jdbc.Driver");
The reason for getting java.lang.ClassNotFoundException can be an issue in loading the driver.Add the *.jar driver to your project if you haven't added it.

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) ~[?:?]

Apache Spark MySQL JavaRDD.foreachPartition - why I getting ClassNotFoundException

I would like to save data from each partition to MySQL Database. For doing that I created Class which implements VoidFunction<> :
public class DatabaseSaveFunction implements VoidFunction<Iterator<String>> {
/**
*
*/
private static final long serialVersionUID = -7039277486852158360L;
public void call(Iterator<String> it) {
Connection connect = null;
PreparedStatement preparedStatement = null;
try {
Class.forName("com.mysql.jdbc.Driver");
connect = DriverManager.getConnection("jdbc:mysql://"
+ "xxx.us-west-2.rds.amazonaws.com" + "/"
+ "xxx", "xxx", "xxx");
preparedStatement = connect
.prepareStatement("insert into testdatabase.test values (default, ?)");
while (it.hasNext()) {
String outputElement = it.next();
preparedStatement.setString(1, "" + outputElement.length());
preparedStatement.executeUpdate();
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
connect.close();
preparedStatement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
And in my main method class I'm calling:
output.foreachPartition(new DatabaseSaveFunction());
I'm getting following error:
15/05/06 15:34:00 WARN scheduler.TaskSetManager: Lost task 0.0 in stage 1.0 (TID 4, ip-172-31-36-44.us-west-2.compute.internal): java.lang.ClassNotFoundException: DatabaseSaveFunction
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:274)
Worker log:
15/05/06 15:34:00 ERROR executor.Executor: Exception in task 1.0 in stage 1.0 (TID 5)
java.lang.ClassNotFoundException: DatabaseSaveFunction
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:274)
Can anybody tell me what I'm doing wrong ? I would be very grateful for that.
Export the external class to jar and add that like sc.addJar("/path/x.jar") where sc is JavaSparkContext in your main. Then you wont get this error. The error is because you spark program is not able to find the class. Moreover in spark 1.3 and greater you can simple use a map options of jdbc and then use load("jdbc", options) to create a data frame and load data from any RDBMS. its really handy. I am not sure if this method works for connecting any RDBMS into spark. Please tell me if you have any other question.

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