I'm trying to use ObjectBox in a simple java server side app.
Everything is working fine, I'm putting things in boxes etc, but the MyObjectBox class is always red when I use it.
I can see the generated .class and .java files, along with the meta _ classes, in build/classes/main/db (db is the package name I have in my actual code), but for some reason I can't import MyObjectBox.
Because of this, I also can't import the _ classes for use in Queries, which now kind of prevents me from getting any further.
My code to use objectBox is inside a class called DB which I have copied below, in case there's anything I'm doing wrong with that.
But as it actually works, I'm very confused!!
Thanks
UPDATE: if I run gradle clean build, my app runs fine, if I run Build->Build Project in intelliJ then I get the error
Error:(27, 21) java: cannot find symbol
symbol: variable MyObjectBox
location: class DB
.
package db;
import java.io.File;
import java.io.IOException;
import io.objectbox.Box;
import io.objectbox.BoxStore;
public class DB {
private File boxStoreDir;
private static BoxStore store;
public DB() {
try {
createMyObjectBox();
} catch (IOException e) {
e.printStackTrace();
}
}
private void createMyObjectBox() throws IOException {
File objectstorefile = new File("../objectBox/objectstorefile");
if(!objectstorefile.isDirectory()) {
objectstorefile.mkdirs();
}
boxStoreDir = objectstorefile;
if(store == null) {
store = MyObjectBox.builder().directory(boxStoreDir).build();
}
}
public<T> Box<T> getBox(Class<T> object) {
if(store == null) {
try {
createMyObjectBox();
} catch (IOException e) {
e.printStackTrace();
}
}
return store.boxFor(object);
}
}
Forget the apply plugin: 'net.ltgt.apt-idea' in build.gradle
Related
My objective is to look at some lines of codes of an external file and count the number of functions of a class are called then.
For example, if I have the following code:
import java.io.BufferedReader;
import whatever.MyClass;
import java.util.ArrayList;
...
...
public void example(){
InputStreamReader isr = new InputStreamReader (whatever);
MyClass object = new MyClass();
someArrayList.add(whatever2)
someArrayList.add(whatever3)
}
In this case, BufferedReader and MyClass functions were called once, and ArrayList functions were called twice.
My solution for that is get a list of all methods inside the used classes and try to match with some string of my code.
For classes created in my project, I can do the following:
jar -tf jarPath
which returns me the list of classes inside a JAR . And doing:
javap -cp jarPath className
I can get a list of all methods inside a JAR whit a specific class name. However, what can I do to get a external methods names, like add(...) of an "external" class java.util.ArrayList?
I can't access the .jar file of java.util.ArrayList correct? Anyone have another suggestion to reach the objective?
The compiler doesn't put the imports into the object file. It throws them away. Import is just a shorthand to the compiler.(Imports are a compile-time feature ).
first step :
use Qdox https://github.com/paul-hammant/qdox to get all the imports in a class :
String fileFullPath = "Your\\java\\ file \\full\\path";
JavaDocBuilder builder = new JavaDocBuilder();
builder.addSource(new FileReader( fileFullPath ));
JavaSource src = builder.getSources()[0];
String[] imports = src.getImports();
for ( String imp : imports )
{
System.out.println(imp);
}
second step :
inspire from that code , loop through your imports (String array) and apply the same code and you will get the methods .
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class Tes {
public static void main(String[] args) {
Class c;
try {
c = Class.forName("java.util.ArrayList");
Arrays.stream(getAccessibleMethods(c)).
forEach(System.out::println);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public static Method[] getAccessibleMethods(Class clazz) {
List<Method> result = new ArrayList<Method>();
while (clazz != null) {
for (Method method : clazz.getDeclaredMethods()) {
result.add(method);
}
clazz = clazz.getSuperclass();
}
return result.toArray(new Method[result.size()]);
}
}
Output :
public void java.util.ArrayList.add(int,java.lang.Object)
public boolean java.util.ArrayList.add(java.lang.Object)
public boolean java.util.ArrayList.remove(java.lang.Object)
public java.lang.Object java.util.ArrayList.remove(int)
public java.lang.Object java.util.ArrayList.get(int)
public java.lang.Object java.util.ArrayList.clone()
public int java.util.ArrayList.indexOf(java.lang.Object)
public void java.util.ArrayList.clear()
.
.
.
All the code - one class :
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import com.thoughtworks.qdox.JavaDocBuilder;
import com.thoughtworks.qdox.model.JavaSource;
public class Tester {
public static void main(String[] args) {
// put your .java file path
// CyclicB is a class within another project in my pc
String fileFullPath =
"C:\\Users\\OUSSEMA\\Desktop\\dev\\OCP_Preparation\\src\\w\\CyclicB.java";
JavaDocBuilder builder = new JavaDocBuilder();
try {
builder.addSource(new FileReader( fileFullPath ));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
JavaSource src = builder.getSources()[0];
String[] imports = src.getImports();
for ( String imp : imports )
{
Class c;
try {
c = Class.forName(imp);
Arrays.stream(getAccessibleMethods(c)).
forEach(System.out::println);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}
public static Method[] getAccessibleMethods(Class clazz) {
List<Method> result = new ArrayList<Method>();
while (clazz != null) {
for (Method method : clazz.getDeclaredMethods()) {
result.add(method);
}
clazz = clazz.getSuperclass();
}
return result.toArray(new Method[result.size()]);
}
}
Output all the methods within the classes imported in the file CyclicB.java :
private void java.lang.Throwable.printStackTrace(java.lang.Throwable$PrintStreamOrWriter)
public void java.lang.Throwable.printStackTrace(java.io.PrintStream)
public void java.lang.Throwable.printStackTrace()
public void java.lang.Throwable.printStackTrace(java.io.PrintWriter)
public synchronized java.lang.Throwable java.lang.Throwable.fillInStackTrace()
.
.
.
You may look into OpenJDK project that has a Java compiler. Learn to build the modified versions. Investigate the syntax analysis layer of this compiler and find where the method calls are handled. Put the logging into these locations and now you only need to build your java file with the modified compiler to get the information about the calls.
The build is complex, but you will likely only need a careful editing in a few files. It is not exactly very low hanging fruit but I think it should be possible to discover these files and make changes in them, and still may be a simpler/cleaner approach than to implement the own Java syntax parser (also doable with JavaCC).
If you also need to track calls from the external libraries, build them with the modified compiler as well and you will have the needed records.
GNU Classpath is another open source project where you can do the similar thing, and it may be easier to build. However, unlike OpenJDK, GNU Classpath java system library is not complete.
This approach may not discover some methods called during reflection. But it would discover that reflection framework methods have been called. If it is a security - related project, the simplest would be to agree that reflection is not allowed. It is uncommon to use reflection in a normal Java application that is not a framework.
When i start the server this error comes :
I am using IntelliJ Idea and MySQL jar is added to the src and in the project modules.
Error pastebin
Here is my code
package com.okaam.jaajhome;
import org.bukkit.plugin.java.JavaPlugin;
import pro.husk.mysql.MySQL;
import java.sql.Connection;
import java.sql.SQLException;
public class JaaJHome extends JavaPlugin {
static JaaJHome instance = null;
MySQL MySQL = new MySQL("address", "port", "schema", "user", "password", "");
static Connection c = null;
#Override
public void onEnable() {
System.out.println("Plugin JaaJHome active");
getCommand("sethome").setExecutor(new SetHomeExecutor());
getCommand("home").setExecutor(new HomeExecutor());
try {
c = MySQL.getConnection();
} catch (SQLException e) {
e.printStackTrace();
}
instance = this;
}
public static JaaJHome getInstance() {
return instance;
}
}
It seems like you're missing on some dependencies, make sure you have them either in the plugins folder as a non plugin or in the classpath to make sure they're loaded.
The following code is for reading or writing files with java, but:
Eclipse prints these errors:
buffer_1 cannot be resolved to a variable
file_reader cannot be resolved
also other attributes...
what is wrong in this code here:
//Class File_RW
package R_2;
import java.io.File;
import java.io.FileReader;
import java.io.FileNotFoundException;
import java.lang.NullPointerException;
public class File_RW {
public File_RW() throws FileNotFoundException, NullPointerException {
File file_to_read = new File("C:/myfiletoread.txt");
FileReader file_reader = new FileReader(file_to_read);
int nr_letters = (int)file_to_read.length()/Character.BYTES;
char buffer_1[] = new char[nr_letters];
}
public void read() {
file_reader.read(buffer_1, 0, nr_letters);
}
public void print() {
System.out.println(buffer_1);
}
public void close() {
file_reader.close();
}
public File get_file_to_read() {
return file_to_read;
}
public int get_nr_letters() {
return nr_letters;
}
public char[] get_buffer_1() {
return buffer_1;
}
//...
}
//main method # class Start:
package R_2;
import java.io.File;
import java.io.FileReader;
import java.io.FileNotFoundException;
import java.lang.NullPointerException;
public class Start {
public static void main(String[] args) {
File_RW file = null;
try {
file = new File_RW();
} catch (NullPointerException e_1) {
System.out.println("File not found.");
}
//...
}
}
I can't find any mistake. I have also tried to include a try catch statement into the constructor of the class "File_RW", but the error messages were the same.
Yes, there are errors in your code - which are of really basic nature: you are declaring variables instead of fields.
Meaning: you have them in the constructor, but they need to go one layer up! When you declare an entity within a constructor or method, then it is a variable that only exists within that constructor/method.
If you want that multiple methods can make use of that entity, it needs to be a field, declared in the scope of the enclosing class, like:
class FileRW {
private File fileToRead = new File...
...
and then you can use your fields within all your methods! Please note: you can do the actual setup within your constructor:
class FileRW {
private File fileToRead;
public FileRW() {
fileToRead = ..
but you don't have to.
Finally: please read about java language conventions. You avoid using "_" within names (just for SOME_CONSTANT)!
javacode already running...thx
same program edited with c++ in visual Studio express...
visit the stackoverflow entry link:
c++ file read write-error: Microsoft Visual C++ Runtime libr..debug Assertion failed, expr. stream.valid()
I am new to Android Studio & gradle. I have an android project that worked partially in eclipse. The original code uses derby database in Android. The old code connects to derby database directly throuhg JDBC. Due to sheer laziness and practicality, I did not change the database to SQLite. Now I want to change the code to use SQLite instead of derby and also, I am porting that project into Android Studio. This is where the fun begins. When I try to run the app, Gradle throws an error saying, " cannot access Referenceable class file for javax.naming.referenceable not found".
Here is the piece of code Gradle is complaining about:
package net.jxta.impl.cm.sql;
import java.io.IOException;
import java.net.URI;
import java.sql.SQLException;
import net.jxta.impl.util.threads.TaskManager;
import org.apache.derby.jdbc.EmbeddedConnectionPoolDataSource;
import org.apache.derby.jdbc.EmbeddedDataSource;
public class DerbyAdvertisementCache extends JdbcAdvertisementCache {
public DerbyAdvertisementCache(URI storeRoot, String areaName, TaskManager taskManager) throws IOException {
super(storeRoot, areaName, taskManager);
}
public DerbyAdvertisementCache(URI storeRoot, String areaName, TaskManager taskManager, long gcinterval, boolean trackDeltas) throws IOException {
super(storeRoot, areaName, taskManager, gcinterval, trackDeltas);
}
#Override
protected EmbeddedConnectionPoolDataSource createDataSource() {
if(!loadDbDriver( "org.apache.derby.jdbc.EmbeddedDriver")) {
throw new RuntimeException("Unable to loadDB driver: org.apache.derby.jdbc.EmbeddedDriver");
}
EmbeddedConnectionPoolDataSource dataSource = new EmbeddedConnectionPoolDataSource();
dataSource.setDatabaseName(dbDir.getAbsolutePath());
dataSource.setCreateDatabase("create");
System.err.println("Created derby cache");
return dataSource;
}
#Override
protected void shutdownDb() throws SQLException {
// annoyingly, shutting down a derby instance involves catching an exception
// and checking error codes to make sure it shut down "normally"
try {
EmbeddedDataSource dataSource = new EmbeddedDataSource();
dataSource.setDatabaseName(dbDir.getAbsolutePath());
dataSource.setShutdownDatabase("shutdown");
dataSource.getConnection();
} catch(SQLException e) {
// make sure we get the correct error codes
if(e.getErrorCode() != 45000 || !"08006".equals(e.getSQLState())) {
throw e;
}
}
}
}
Could you guys help me ?
Thanks
D
There are several problems with Derby on Android including the absence of the naming support.
See the 08/jan/2010 comment on https://issues.apache.org/jira/browse/DERBY-4458
Here is my code:
import com.bmc.arsys.api.ARException;
import com.bmc.arsys.api.ARServerUser;
public class ARServer {
public static void main(String[] args) {
ARServerUser ar = new ARServerUser();
ar.setServer("ServerName");
ar.setUser("Username");
ar.setPassword("Password");
ar.connect();
ar.login();
try {
ar.verifyUser();
} catch (ARException e) {
System.out.println(e.getMessage());
}
}
}
I have created build path for this jar file "ardoc7604_build002.jar" but still i am getting errors like:
import com.bmc.arsys.api.ARException can not be resolved
import com.bmc.arsys.api.ARServerUser; can not be resolved
ARserver can not be resolved
ARException can not be resolved to a type.
Thanks in advance for help.
you have put the javadoc jar in your path - you need to use the java api jar instead.
arapi7604_build002.jar
are you Only Java person or do you understand C# aswell if so I can give you some examples