How to import source from GitHub into an Eclipse project - java

I am trying to use this tool from Google: https://github.com/pcj/google-options
In the example given by Google, they use:
import com.google.devtools.common.options.OptionsParser;
to allow their example code to use the command line parser.
When I do this, Eclipse says the referenced files don't exist.
I have tried putting the source file from google-options in a package in my project; no luck. I tried running Google's example, and it won't even compile. It can't find all of the classes in its own src folder.
Based on my research, I think there's either an issue with a .jar file needing to be in the build path, something to do with Apache Ant, or an issue with GitHub I don't understand.
I'm very new to GitHub; this is the first time I've tried to do anything like this.

You should first import this project from GitHub to your local machine. import statement in your class means that classes from other packages should be imported to this class you're working on. Import not from the remote source, but from local.

I figured it out. It needs the guava.jar file found here added to the buildpath. After that, it has a couple weird dependency issues with javax, but I just removed those (it was Nullable and Immutable, if anyone's interested).

First you must clone the project and compile it, to do that put the following commands:
Open your terminal and go to the workspace folder:cd {workspace}
Clone the project: git clone https://github.com/pcj/google-options.git
Compile the project: mvn clean install
Check the result, this should print: BUILD SUCCESS
In that step, maven will download all dependencies that the project needs.
To import the project: Launch eclipse, click on File /Import... choose Existing Maven project, browse your workspace where the projetc was cloned.
Create a class that extends OptionsBase and defines your #Option(s). For more information about how to use, see https://github.com/pcj/google-options#usage

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

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

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.

Apache Spark Java examples with IntelliJ: sql package does not exist when make

When I create a Scala SBT project in IntelliJ, everything works fine. I set up the jar dependencies and I can run the examples.
But if I create a Java project and try to run:
Error:(29, 28) java:
C:\workspace-java\SparkExamples\src\org\apache\spark\examples\sql\JavaSparkSQL.java:29:
package org.apache.spark.sql does not exist
I added that jar to module dependencies which contains the class in the error message. In the IDE everything seems fine, I can navigate into the JavaSparkSQL class. But when I make, it's doesn't find that class.
I added the jar to CLASSPATH, and also copy to JAVA_HOME/jdk/jre/lib/ext.
What do you think what do I wrong? What should I check?
UPDATE:
The problem is only with org.apache.spark.sql package. There are other packages what I can use. (org.apache.spark.SparkConf; org.apache.spark.api.java.JavaRDD; etc.)
UPDATE 2.:
I gave a try to Eclipse Mars, and it was completed successfully the first time. So it is an IntelliJ specific problem.
UPDATE 3.:
Something wrong with the jar file which comes with Apache Spark (spark-assembly-1.4.1-hadoop2.6.0.jar).
Eclipse has no problem with this jar.
I found that when I delete an irrelevant file from jar ("about.html") then the problem is gone. IntelliJ see every package from the modified jar.
How is this possible?

implementing the NWalign.java alignment script for needleman wunsch algorithm

I'm trying to run the NWaling.java file found here. the code present at the link that I mentioned makes use of the jaligner library. I have downloaded this Java package from here. inside the zip folder there is a JAR file. I'm developing the code using eclipse. whenever I add this JAR file to the code there are some classes missing for example:
import jaligner.NeedlemanWunschGotoh;
&
import jaligner.matrix.MatrixGenerator;
also in the entire zipped folder there isn't any pom.xml file to allow me to import the entire folder as a maven project to eclipse. can somenone suggest me how to import this jaligner library and use it in eclipse? thanks.
You can download a community implementation of the jaligner algorithm from here.
Take a look at this project sources where you may perhaps find all the searched artifacts.
The full Eclipse project with the NWalign.java can be found here including .classpath file with all dependencies listed there.

Building Android Source apps In Eclipse

I downloaded the source to spareparts here
https://android.googlesource.com/platform/development/+/gingerbread-release/apps/SpareParts
It includes Android.mk, AndroidManifest.xml, Notice, res and src folders.
My question is how can I compile this source code in Eclipse? I see there is a make file (Android.mk) which I'm guessing I need to use but I can't figure out what to do to get this thing imported into Eclipse without having 500+ errors.
This article seems to have the answer. According to it you would need to download all of the source just to build one of the apps, though.
Brute force always works :)
Create a new android project in Eclipse named SpareParts
Use the explorer to replace both the src/ and res/ folders inside workspace/SpareParts with the ones downloaded form git
Click with the right button on the SpareParts entry in the project tree in Eclipse -> Refresh
enjoy
I looked into the files and it does not seem that it uses any external library.
If you try to build it by itself, you will still have problems due to these three classes that are not in android.jar:
import android.app.ActivityManagerNative; //cannot be resolved
import android.os.ServiceManager; //cannot be resolved
import android.view.IWindowManager; //cannot be resolved
In Eclipse try File->Import... and General->Existing Projects into Workspace, add the project folder and then try to compile it. Hope it helps.

Categories

Resources