Android Studio: How to import java class from outside the project? - java

I'm working on an android game that requires a server. We've set up a git in which there are three folders: Client, Server and Shared:
Client contains the Android-Studio project.
Server contains an Eclipse project which is our server and database.
Shared contains java classes that I would like to use in both the Eclipse and Android-Studio project.
In Eclipse I successfully loaded the files from Shared by importing them as "file system". (It created a package and everything works great)
In Android-Studio, I can't find a solution that doesn't require me to copy-paste the classes in the project. The aim here is to have those file at only one location as they're susceptible to change but must me identical in both the projects... So those solutions don't fit my needs.
I'm looking for something like a way to link files from outside the project. (I managed to link libraries from inside the project already but not from outside)

The simple solution is to copy the desired java files into your project structure. Of course, you may have to change package names manually.

Found a solution that might not be optimal but at least in my case it solved the problem.
In Android-Studio:
Create a folder called something like "CommonEclipseAndroid" following a path similar to this one: project/Android/app/src/main/java/com/example/myapplication/CommonEclipseAndroid
Paste the code you need to use in both project inside this folder.
In Eclipse:
Right click on your project: Properties > Java Build Path > Source > Link Source and choose the folder we've just created.
Now your projects use the same code. You only need to modify it once for both projects and there's no risk of error.
Hope I helped :).

You can import the shared folder as a module .
go to file> new> import module
Now use the same imported folder in eclipse as well.

Related

IntelliJ ctrl-click references a .class file not a .java file

I have the following line of code in a .groovy file for testing:
GenerateShipConfirmsForBatch gscb = new GenerateShipConfirmsForBatch();
Ctrl-clicking on the GenerateShipConfirmsForBatch takes me to GenerateShipConfirmsForBatch.class in a .jar, and not the .java file, even though I have the class correctly imported at the top. I want it to reference the .java file so it will pick up changes I make to the .java file.
Any help would be greatly appreciated!
First guess - Wrong import
What you wrote seems to me like you have class with the same name in 2 different packages or in imported project instead of in open-able java class. When you import class be careful that you import the one you want to use.
Eg. annotation class Service is wildly used in different packages.
import org.springframework.stereotype.Service;
import com.google.web.bindery.requestfactory.shared.Service;
Just check that you are using the correct import.
Second guess - Incorrectly set modules
If you are having the multi-module application you have to set correctly the parent project to properly address this issue as well as child projects where the links should be as well.
In Maven it is done using pom.xml. It is very nicely addressed in Maven - Guide to Working with Multiple Modules.
In Gradle it is done using build.gradle. You can read more about it Gradle Multi-Module Project Setup.
Basics about classes
Local class
Idea is linking local .java files in preference instead of .class therefore if this is happening I'd recommend reinstalling Idea as I cannot find the correct approach.
Linked class (from external library)
If you have imported external library it WILL link to .class as it is decompiled from .jar file.
What you can do is either download .jar with source codes, if you are using Maven Projects click on Download Sources and/or Documentation.
Just because you have the class imported at the top does not mean that you can view the source code (e.g., .java file). If this class is coming from a dependency defined in your pom.xml or build.gradle file then you likely won't have access to view the source code. However, if this is a separate module you have at the top level of your project, then you'll be able to view the .java file. If this library is open source then I'd suggest cloning it in your project and adding it as a module. That will solve your problem.
You can install Java Decompiler IntelliJ Plugin from here: https://plugins.jetbrains.com/plugin/7100-java-decompiler-intellij-plugin
It allows you to display all the Java sources during your debugging process, even if you do not have them all

Importing .java and .class file gone wrong in eclipse

Recently I worked on a project on a mac, using eclipse. When I was done with the project I copied certain .java files and their corresponding .class files onto a USB drive and brought it to my personal computer. When I try to use the import feature on eclipse and import everything, my main method is not recognized by eclipse. It will work if I create a new eclipse "class" by the name I have in the program and copy paste the code. I have many classes so I can not do this for each one. Is there any way for me to change all .java/.class files into files that will be read by the system?
Thanks
An eclipse project contains a lot of meta information. Just open the project directory in a file browser, and have a closer look. There are files like .classpath for example.
When you just copy your source code, you leave all the meta information behind!
You should either copy the complete project directory, or simply use the export task to create a ZIP file of your project. (to later import that in other systems).
There is one other way you can do this. Just create a new project with the SAME EXACT PROJECT NAME that you did at school or wherever. After you create your project, go to your file explorer and navigate to the following place:
<"Directory to work-space">/<"Project Name">/src/
For ex: in Windows, it is Generally:
C:/Users/your_name/workspace/projectName/src
In here, copy all your ".java" files.
Now go to eclipse, right click on your project and hit refresh. You will find all your .java files there. Right-click on your project and hit "Run"->"Run as a Java Project". It should run! hope this helps.
PS: You do not need your class if you are executing it in Eclipse. Eclipse will create its own class files.

Adding External jar copy to Project

I created a project but I am using an external library for it. enter link description here
I added the library to my Beans as follows:
Right click on folder "Libraries"
Add Library
Create...
Gave it a name "gson 2.7"
Picked the class, source and javadoc files.
I went on and continued programming and then I added my code to Github. Then when I cloned it my Library was missing.
How can I fix this ? How can I make it dynamic so I dont have to worry about this anymore. In other words I need to add the library to my code and not just the reference.
Thanks a lot for the help
Just upload a copy of your library to github.
I have not used netbeans, but from what I understand it adds the jar somewhere outside of your project directory, and it simply adds the location of the jar to your classpath. When you upload and then clone the project from github, the jars are not uploaded/cloned, and the project structure metadata for your project is also lost from netbeans.
My suggestion is to learn and start using a build tool like maven. It takes up the responsibility of adding your dependency libraries everytime, given that you have an internet connection. It also adds the libs to the target directory of your project folder, so you probably will need the internet connection only once. The libs will be uploaded and cloned back from the github repo everytime.
IDEs like eclipse have inbuilt support for maven, so you don't even have to worry about executing the mvn commands. Here's a tutorial that will get you intrigued - with the IDE that you prefer.
https://platform.netbeans.org/tutorials/nbm-maven-quickstart.html
Thanks a lot for all the replies and comments.
Technically there are two approaches to my problem. For this articular example I chose to copy the external jar files to my project.
1. Copy jar files to your project.
Go to project properties
Under Libraries folder make sure to set up a libraries folder by following the steps.
Add Library at the bottom ( you can create or import; since I already had gson in Netbeans I just imported it.)
Press ok to confirm and you are done!
2. Use Maven
Go to plugings and make sure you have all Maven plugins installed, if not do so.
Create an new project and choose maven/java application
Follow the prompts
At this point you can start coding (in my case I just imported my class files from the other project)
Right click in "Dependencies folder and then "Add Dependency"
Under query type the repository you are looking for; in my case "com.google.code.gson : gson : 2.7"
Click add and you are done!
Thanks a lot to everyone for the help :)

Jar file too big

I created a simple project using WindowBuilder in Eclipse and my goal is to send it by email. I don't know what kind of computer the person is using, so I exported my project into a 'Runnable JAR File' and checked the option 'Extract required libraries into generated JAR'.
The problem is that the generated JAR file is 20MB in size!!! The project has only one simple window - nothing complicated or fancy.
I found that some people use ProGuard to include only what is really needed.
I would like to know if there is a way to optimize it 'manually'? Are there libraries that are automatically included when creating a WindowBuilder project, and how may I determine which libraries I can remove?
Thank you.
I've had the same Problem using WindowBuilder. My solution was to look for the imports in my .java file, e.g.: import org.eclipse.swt.SWT;
In the Project Explorer in Eclipse you can see that there are much more imports than needed. Those 'Build Paths' can be removed carefully.
Simply rightclick on a .jar import like "com.ibm.icu_52.1.0.v201404241930.jar" and click on "Build Path" and "Remove from Build Path". Unfortunately, you can't remove or delete the packages inside the .jars.
But using this method, I managed to reduce the size from 20.7MB to 3.87MB. It's something...
Like fge said, send the source. Or upload the .jar to dropbox if you require you partner to access all of the files in the .jar.
If you don't like dropbox, find some other cloud storage solutions through a google search. Github or Bitbucket could also be an alternative to dropbox.
Cheers.
Use the Eclipse plugin FatJar. FatJar shows you which libraries are getting imported and allows you to specify which .jars, libraries, etc., that you want built into your JAR.
FatJar
Your jarfile is too big due of the project template. Create a sample java project, then use windowdbuilder components. Do not create a project Swing Template, by this way eclipse is gonna import all eclipse gui toolkits.
Tried it out with my way, my application's length equals to 3Ko against 24MO with the template project

How to open existing Java 'solution' (not developed in Eclipse) using Eclipse?

I've been tasked with picking up someone elses Java code and adding some functionality to it.
I'ved pull down the source tree from CVS and see a bunch of .jar files in different folders. I'm guessing the developer did not use Eclipse.
I am new to Java (coming from .NET background) and have used Eclipse so far to create one Java project. I'm wondering now that I have this guys files (he has classpath.jar and other .jar files along with some subfolders each with 'java' files in them), how do I open them? I tried opening one at a time, etc.. but doesn't seem to work. IS tehre an easy way to do this? I thought there' might be some kind of 'import existing code' thing in Eclipse but I can find it. How can I do this? Do I re-create the folder structure and just add the existing files one a time?
Thanks much for any help
something like 'create project from existing source'?
http://www.stanford.edu/class/cs108/JavaTools/eclipse-guide/
if the existing code is not structured well, you are either going to have to heavily configure your project sources, or just change the project structure.
File -> new
Than select general->folder.
To make developing easier in eclipse i recommend some refactoring to the project.
create a new eclipse project using the parent folder as the home.
every folder that's the root of a hierarchy of java classes becomes a folder in the "source" tab (either on creation, or add through "project->properties").
every jar (at least the ones he's using, there may be extras) gets added in the project->properties libraries tab.
This is assuming that all of the hierarchies belong together and that the thing isn't structured to build little sub-projects out of pieces of the hierarchy. If there's a build file for this thing you might want to be sure that if the build file is doing that you're building things appropriately.
File->Import->General->Existing Projects into Workspace
OR
File->New->Java Project
This will create a sample java project for you. You can add the files appropriately.
Below is an example of a j2ee Project:
http://java.sun.com/blueprints/code/projectconventions.html
If C:\Workspace is the folder you are using as the workspace and you have your existing project placed as "C:\Workspace\ExistingProject"
Open Eclipse Got to File->New Project
Select the type of Project you want to create Use the name as "ExistingProject" for the project and click Finish or complete the remaining steps of project creation wizard normally.
Internally a .project file would be created in the ExistingProject folder and a .metadata folder would be generated under Workspace folder.
If you want to place the ExistingProject not under the workspace u follow the same steps.
There's 2 possibilities:
Import project from file system:
Create a blank Eclipse Project.
Then select File -> Import -> General -> File System. Select project, and point it to your created project.
Importing from CVS
Goto File -> Import -> CVS -> Project from CVS
Enter your CVS Host, Repository Path, Username and Password, and click next,....
Add what you need and click next (follow the instructions) until you're satisfied and click finish.
Hope this helps.
A simple tutorial that incorporates my 1st option and commmits it to CVS: http://thedesignspace.net/MT2archives/000662.html
Two options:
Maven - highly recommended but rather read this: http://maven.apache.org/guides/getting-started/maven-in-five-minutes.html than have me re-write it here. Maven might seem like more effort up front but it pays for itself a hundred times over during the course of even a simple project.
Do it yourself (assuming Helios):
Move the source code Java files to ~/development/MyProject/src/java. Move the jars to ~/development/MyProject/resources.
In Eclipse, File > New > Java Project. Type in your project name.
Untick "Use default location" and browse to ~/development/MyProject.
Select src/java as your source folder (if Eclipse doesn't pick it up automatically).
Finish.
Then, for each error, you will need to find the corresponding JAR and add it as a library to your classpath in the project properties.
The important thing to bear in mind is that Eclipse is not like Visual Studio - you cannot easily just edit one file at a time and that is not what it is designed for. People can get frustrated with Eclipse after working with VS but if you just allow it to do things the way it wants you, your life will be much easier.
trick is finding the root folder. Generally, developers use the following:
project root
-- src
-- bin
at least, what's what Eclipse does by default. There are other ways it can be organized as Maven uses the following:
project root
-- src
-- -- main
-- -- -- java
etc...
More info on how Maven standardizes here:
That said, finding out how the source is organized shouldn't be too hard. Open up one of the .java files and look for the line at the top that starts with "package ". Should be something like this:
package com.somecompany.client.utils
Note, that's just an example, it won't be that exactly although it should start with "package". The value after package represents the path that the file should be in relative to the root of the source folder.
source
folder/com/somecompany/client/utils
So, if you follow the default way that Eclipse organizes things, it should look look like this:
project root
-- src
-- -- com
-- -- -- somecompany
-- -- -- -- client
... etc
SO, as other people have mentioned, you'll import from existing filesystem, point to the folder at the project root. You may need to configure it to point to "src" folder as a source folder. You may also need to import those .jar files into your project as well.
Good luck

Categories

Resources