External class folder not exportable? - java

Eclipse, Android project. I have a library project (mylib) and an app that references it (myapp). I've added to the build path of mylib a folder with a bunch of compiled Java classes (com.foo.*), checked it as exported. Added a call to one of those classes to the main activity. There are no build errors. But when I run myapp and try to call a method from com.foo, I get a "class not found" exception.
When I package the same class folder into a jar file and reference/export that in Eclipse, the app works.
What am I doing wrong? Are class folders not subject to export somehow?
EDIT: here's my thinking. The Java Build Path window treats JAR's and class folders uniformly. Any why shouldn't it - a JAR is, for the most part, just a class folder zipped up. But then there's this discrepancy of behavior when passing classes on to other projects. Unexpected...

You need to make it an android library and import it as such in the android panel

Related

IntelliJ can't resolve symbol despite adding library

I am creating an external library to share code between my client/server programs. IntelliJ can't seem to import this jar file and I can't understand why, I followed all the advice I've found online.
I added the jar to my lib folder and added it as a library in my main module (it's called 'common':
This is the error (repeated on each instance of me using User or Packet). On import statements it says "package common does not exist":
I've tried invalidating caches and that didn't seem to work either. I tried this same import on a new project just to see if I was doing anything wrong and it worked perfectly on that new project so I'm not sure what's going wrong here.
EDIT:
I've noticed that this error only happens in files that are in packages within the src folder. So basically, Main can find the jar because it is only in the src folder, but RegistrationController can't because it's in src/communication.
The problem was that the shared code I was making was not in a package, just a src folder. Because of that, the code that was in my default package (src) could access the classes, but not the code in packages. To fix this, I put the shared classes in a package (com), exported them as a jar, and imported that into my project. Now, my packages can do com.Packet or com.User and get the files successfully.

Accessing files in a project that is on the build path of another

I'm working on a framework-type project in Eclipse, that has a res folder attached with, as you guessed Resources inside of it. At the moment, they are crucial text-files that are to be read.
I also have a main test rig that is apart of it's own Eclipse project, as I plan to expand it in the future. This test rig has included the framework project on it's Build Path, so I can access classes from the framework as they are added.
But this is where my problems lie: If I want to import a class, that works fine. If I want to access a file through an InputStream, or Reader, it breaks. I know this is because the files included from the secondary project aren't placed inside the test rigs bin folder, rather they reside in their own bin in the other project.
Is there a way I can get my main project to access resources from the other without having to do ../../ trickery in my file paths? I plan on making this a real thing and don't want file paths doing things like that.
In eclipse do this,
Build Path ---> Configure Path ----> Click Project Tab ---> Add the project.
Also, you can package the other project in a jar and import it as an external jar.

Handle GWT super-source in Eclipse

I want to emulate some Java classes and therefore I use the super-source tag in a gwt.xml module.
I created two eclipse projects one only for the super sources and another which contains the app code.
The super source project has the following structure:
/
/super/Emul.gwt.xml
/super/emul/EmulatedJavaClass.java
Emul.gwt.xml contains:
<super-source path="emul"/>
In eclipse I right clicked on the super folder and added it to the Build Path. That works fine. The problem is that eclipse shows an error on the package tag of my emulated java files.
In my emulated package I cannot use:
<super-source path=""/>
This will give an error while compilation.
Can I do something that Eclipse does not show an error in the package and still have all the nice eclipse features like code completion?
There is no enough info about your complete structure in eclipse, but normally:
Your module.gwt.xml should not be in the super path but in the normal folder for sources, ie: src/com/example/Module.gwt.xml with the entry <super-source path="super"/>
Then you can put your super folder in the src, ie: src/super/com/example/MySuperClass.java
In eclipse you have to configure in your project class path src and src/super, but when configuring src there is an option to put exceptions so set src/super as exception.

How do I know the path for Java import?

I'm trying to learn Java ( I know php ) but I don't know what path the included classes have. For example, I create a new directory in Eclipse (in the package) and drag there a class from other project. When I try to import it, it cannot find the class. Even if I don't have any dirs and the class is directly in the package, using import package.classname doesn't work...
I must be missing something but googling doesn't show me any replies.
How do I get the class path? Is it somewhere in the properties?
Java has the concept of a classpath: a path where all classes should be found.
You can get the existing classpath with this code:
System.getProperty("java.class.path")
If you run java from the command line then you have to set your own classpath with your classes.
From the classpath, use the package to find the class. For example, if the classpath is ".", which is the current folder, and you have a class called A, which is in the package com.yourcompany, then you will find the class under ./com/yourcompany/A.class
On the example you gave, go to the terminal and look for the "bin" folder and you will see all classes. However, if you want to add a new class from another project to your project, then there are simpler ways. You can simply open your build path in Eclipse and add the class from the other project onto your project.
Another way is to create a jar from the other project and add the jar to your project.
In Eclipse, go to Project->Properties-Java Build Path where you can config the classpath which allow you to import.

Use .jar with referenced file paths in other project

I have 2 Java projects, one is a Web Project in NetBeans and the other is a Java Project in Eclipse.
Just to know, the Java Project from Eclipse is used by the Web Project in NetBeans as a library (.jar)
Situation:
1. Java Project from Eclipse has the following structure:
And inside the src, there is a class that uses the file1, file2, etc in some method using global variables like this:
public static final String PATH_ONE = "./files/file1.xml";
public static final String PATH_TWO = "./files/file2.xml";
...
Finally, when I test the method in some main() class everything works good.
2. Web Project from NetBeans has a jar reference to the above library and If everything works good, then the web project will be able to execute the method that uses the global variables without problems from the library project.
Problem:
When I run the web project and I want to invoke the method from my library that uses the above xml files from it, for some reason, NetBeans or the project (I don't certainly know) looks for the path and fails in Exception because the path can not be found (It seems that tries to find the path in the web project and not in the library one).
How can I solve this issue? It sounds simple but I don't want to change my structure, load them as a resource or transfer files from one project to another and use external paths to make this work because I just make a recreation of the situation but I am working with lots of files with different folders and paths too.
If you unzip your jar you can't find your folder file because not is included in your classpath.
You can See .classpath file and the folder isn't. For these reason is FileNotFoudnException.
You can to add how source folder in Eclipse ID:
1. Right click in your project/Build Path/ New source folder
Create the source folder . Then you can add a new package with the name Folder.
Your project:
src
resources
folder
resource1.xml
resource2.xml
2. Modify your java project.
public static final String PATH ="/folder/resource1.xml";
Now, if you try to unzip the jar, you can see that the folder was added in the .classpath.
It works for me. I tried to paste image but i'm new user.

Categories