There is DLL file called as myAPI.dll. It contains classes that I'd like to use in my JAVA code. How to import this DLL file into my Java project in Eclipse? It should be possible to run the code based on something like this:
import myAPI;
public class MyClass {
//...
}
}
I do not know which language is used for your code snippet but it is definitely not java. Java does not have keywords like using and namespace.
Generally to call native code from java you have to use good old JNI or newer JNA.
Please follow the following links to get started with these technologies.
http://java.sun.com/docs/books/jni/html/start.html
http://www.javaworld.com/community/node/1767
The code snippet you have shown is C#, not Java. Now namespace lets you group logically related things. For e.g. all order processing related classes can be put under single namespace. It is also used as a mechanism to avoid/resolve name conflicts. It also defines the visibility scope of your class. Read this lesson for more details.
Related
Is there a way to get Java package and classname from which native library was initialized from JNI_OnLoad? I want to reuse my native library in multiple Java projects and don't know in advance classname and package from where LoadLibrary("mynativelibrary") is called. Then I could use JNI RegisterNatives with dynamic classname.
I've come across the same problem recently. I ended up reusing the class (i.e. copying the code) which loads the native library and handles all callbacks into other projects keeping the original package name. If you make that class generic and use an interface for callbacks then it shouldn't have any dependency on the project and so can be used anywhere. Works well for me.
I would like to manipulate Java classes (with java extension not .class) so that I could :
Delete all methods of a class (keeping the constructor)
Add unimplemented methods
Remove unused imports
...
Is there an API that could accomplish this ?
What I've done so far is trying to manipulate the .java files like text files (with regex,FileUtils, etc.).
Regards.
I
You could look at using the AST (Abstract Syntax Tree) tools from the Eclipse JDT project.
There is a tutorial to get you started at Vogella: Eclipse JDT - Abstract Syntax Tree (AST) and the Java Model - Tutorial
If you only want to temporarily modify the classes (i.e. within the scope of the jvm) then you could do this with reflection:
What is reflection and why is it useful?
If you're taking about permanently altering/creating source code then this is maybe best done using an IDE. Most IDE will tell you about unimplemented methods and provide auto completion to create them. They will also format the source code, remove unused imports etc.
You can use a regular expression, the question then is then what regular expression (And what other options are there!)
Regular expressions maybe aren't ideally suited to this, and for example, when it comes to another task they're not ideally suited to, such as parsing XML, people say don't do it, use an XML parser, but in this case, if you find that there is an absence of a tool built for parsing java source code, then regular expressions may be the best option.
Yes, you can use java reflection api. Please check here
Later edit: To update the class structure you can use javassist. Here you have an example.
I'm working with an API that generate a lot of java code to me.
But this API does not handle import correctly, so it write full qualified name of every class. eg:
public class Foo{
com.my.company.Bar bar;
public com.my.company.Bar getBar(){
return bar;
}
}
I would like to find an API to post process this generated code and write something like that:
import com.my.company.Bar;
public class Foo{
Bar bar;
public Bar getBar(){
return bar;
}
}
Is there any known API able to do that?
Filtering imports from existing code isn't trivial; imagine you have two classes with the same name but different package.
My usual approach is to have a helper class which manages the imports for me. In the generator, I can
String type = importSet.add(Foo.class);
type is then used in the method to access the type. The import set collects all imports and handles duplicates.
For this to work, you need this "main loop":
importSet = new ImportSet();
String body = generateClass();
out.write(importSet);
out.write(body);
i.e. you need to generate all the code for the class itself (collecting the imports as you go) first. Then you write the imports to the file and after that the generated class body.
If you want to change the sources, I suggest to use the Eclipse Java compiler because it can give you the AST of the code. You can then apply various transformations on this tree. I have an example in my blog how to get the AST.
As far as I see you expect to generate Java code right? If yes . We are using Eclipse JDT in our project which is from Eclipse IDE and they use it for Java code generation. And I encourage to use it, however depending on need you might go for simple solution like QDox or even other solution.
You can consider either one of the solution
Eclipse JDT
Javaparser
Qdox
Eclipse JDT
Pros
Impressive functionality
Very rich API
Support for Java 7 features and they have a plan to also support Java 8 features
Localizable syntax error messages
Con's
Steep learning curve
Resources
Intro to Eclipse JDT
http://www.eclipse.org/articles/article.php?file=Article-JavaCodeManipulation_AST/index.html
How to use JDT API outside eclipse
How can I use the java Eclipse Abstract Syntax Tree in a project outside Eclipse? (ie not an eclipse plugin)
Nice example and project on AST
http://www.ibm.com/developerworks/opensource/library/os-ast/
Access Eclipse jar plugin sources
http://www.vogella.de/articles/EclipseCodeAccess/article.html
Test samples
http://git.eclipse.org/c/jdt/eclipse.jdt.core.git/tree/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests
Qdox
Pro's
build from scratch and modify
lightweight
FQN type based searching
clean and readable API
extensive querying possibility like isFinal(), isPrivate()
object oriented constructs, almost everything in a java file can be represented in terms of objects like, JavaClass, JavaField, JavaMethod, JavaParmeter
Con's
last public release was 1.10 on 2009-09-04, how ever 2.0 snapshot is available
1.10 is not supporting static import;
writing to a file is pre-formatted, don't have much control over it, can't specify the line numbers
no article or tutorial, source code is the only reference
Resources
- Qdox http://qdox.codehaus.org/changes-report.html
Firstly, I'm trying to learn Java with Java, A Beginner's Guide, 4th Edition. I use Ubuntu as my OS and Netbeans as my IDE. I need to create a project to create a class when using Netbeans.
Here is my hello world app.
class First{
public static void main(String args[])
{
System.out.println("Hello!");
}
}
But this returns a lot of errors. When I put package first; line to top line of my Java class it runs. But, the book isn't using package term. Is this a rule of Netbeans, or what I need to know about this?
You never need to put a class in a package. However, it is almost always a good idea. This is something that Netbeans aggressively tries to encourage you to do.
For small projects, using the default package (that is, a file without a package statement) is OK. However, once your project grows and starts adding external libraries, bad things can happen. What if someone else decided to use the default package and happened to have an identically-named class? Now, since you're both in the same package, collisions can occur!
Your file structure should also reflect your package. That is, a file in package com.myurl.helloworld should be located in a folder called com/myurl/helloworld. This is for the same reasons as above.
Also, and you probably haven't gotten here in your studies, you cannot import classes from the default package, or use the package-private visibility modifier in a default package class.
That's because the author of Java, A Beginner's Guide, 4th Edition most likely used the "default package". This way, you don't have to include any package. A package is a namespace declaration.
What the heck is a namespace declaration!?
A namespace declaration is simply a package which is made to organize your classes. For an instance, if you're going to have multiple classes for, let's say your GUI, and multiple classes for algorithms, blending them together is always a bit confusing. Sorting them in different packages, however is a superior solution.
There is also a naming convention which you should follow if other people are going to look at your code. Packages should be named after a top-level domain. I tend to create SourceForge projects and then I end up with something like this:
net.sourceforge.softwarename.security, net.sourceforge.softwarename.gui, etc...
Also note that you should never use upper case when naming your package. More info here.
You're going to encounter lots of situations like these when learning to programming. They're all a part of the game. You'll just have to figure out a bit by yourself.
The best I can do for you is to recommend Eclipse. Also, when learning Java, I would suggest that you do not use an IDE at ALL! That's because you'll learn to code independently. It's up to you, though.
No you don't need to put in a package into your class UNLESS you are going to import it to another class that will be in it's own file. This is where protected type variables come in when you don't want to make them priviate but only want the subclasses (or child classes) access to them. You are also missing your public statement for your class so it should look like
public class First{
public static void main(String[] args){
System.out.println("Hello!");
}
}
Package represents a directory that contains related group of classes and interfaces.
A package is a namespace that organizes a set of related classes and
interfaces. Conceptually you can think of packages as being similar to
different folders on your computer. You might keep HTML pages in one
folder, images in another, and scripts or applications in yet another.
Because software written in the Java programming language can be
composed of hundreds or thousands of individual classes, it makes
sense to keep things organized by placing related classes and
interfaces into packages.
Below you can find some good discussions regarding java packages:
Java packages com and org
Are there best practices for (Java) package organisation?
Java com.* package namespace
I was doing some experiments today in android source.
Let me tell the complete thing,
I compiled framework.jar from android source and decompiled it and generated smali source and kept it aside. Then from CyanogenMod repo I added the commits of a feature to android source and compiled framework.jar again and again decompiled smali source to see the changes in terms of smali so that I can port them over to my ROM.
The feature I am porting requires importing of certain classes e.g import dalvik.system.VMRuntime and extra coding for utilization of those imported classes. So now my problem is, I am only able to see the extra coding i.e utilization of those classes in the smali code but not those imports. So when I port only the smali code I get java.lang.NoSuchMethodError in logcat which shows that it is unable to find that method. The reason is clear because the necessary class is not imported then how to do it in smali code? i see no way to do that in smali and due to which the newly introduced methods don't work.
Any feasible solution to this?
The only thing an import does in java is make it so that you can mention a class without having to specify the full package name. In smali, there are no imports - everything always uses the fully qualified class name that includes the package.
As such, your problem is definitely not related to imports. It sounds like you are trying to use a method that simply doesn't exist on the device.
You can disassemble the framework jars from your device and find the definition of the dalvik.system.VMRuntime and see what methods are available. Or alternately add some reflection calls and log the info to logcat.
It's worth noting that VMRuntime is not part of the public API, and it may not be present or consistent on all devices or future versions of Android.
(I don't know smali, so there may be a much better solution)
No Java program ever requires any import statement. To use e.g. ArrayList you need to either import it or refer to it in full, as java.util.ArrayList.
There is a significant difference between e.g. a C++ #include and a Java import. A C++ #include directly inserts code in your program, typically the declaration for a class you are using. The process of getting the declarations is divided into two stages in Java. First the compiler determines the fully qualified class name, then it uses its own library and the classpath to find the declaration for that name. Java import is used only in calculating the fully qualified class name, and so is not needed for any class that is only referred to by its fully qualified name.
Perhaps you could pre-process the code you are adding to replace e.g. VMRuntime with dalvik.system.VMRuntime etc. so that you can compile it with no imports.
Here is an example of a short program that uses java.util classes, in different ways, without any import:
public class Test {
public static void main(String[] args) {
java.util.List<String> list = new java.util.ArrayList<String>();
list.add("bbb");
list.add("aaa");
java.util.Collections.sort(list);
System.out.println(list);
}
}