Use a java class from a referenced project on the server-side? - gwt

I have a GWT project, and I want to use some classes server-side from another project:
MyServerProject
com.me.myserverproject.server.Horse
SomeSupportProject
com.me.somesupportproject.server.Animal
and MyServerProject Horse.java looks like:
class Horse extends Animal {
}
In eclipse, I have MyServerProject referencing the SomeSupportProject project. All compiles fine, and GWT Compile runs fine too, no errors.
When I deploy MyServerProject to a local instance, it immediately throws a NoClassDefFoundError error on "Animal.java":
java.lang.NoClassDefFoundError: com/me/somesupportproject/server/Animal
at java.lang.ClassLoader.defineClass1(Native Method)
I marked SomeSupportProject as exported in the eclipse project properties. But it seems like the class file for Animal.java is not getting exported upon deploy. Do we need to do something special here to get that to work?
I must be missing something really obvious since this is pretty basic stuff. I have clientside java files being referenced just fine, which I think is the trickier case. But these are all server-side classes, thought it would be simpler,
Thanks
------- Update: Project Setup ---------------
Some notes on my environment:
Using eclipse, and the two projects are side by side. I'm not using Ant or Maven. I have eclipse set to build automatically, so not getting any compiler errors there. To actually compile for a deploy, I tried right clicking "MyServerProject", choose Google -> GWT Compile. I set output to "all". I don't get any compile errors. The output does not mention any warnings.
After GWT Compile is complete, I right-click the project again, choose Run As -> Web Application. This is what throws the java.lang.NoClassDefFoundError for the class found in the SomeSupportProject project.
If there any specifics that would help just let me know.
--------- Final Update: Solved -----------------
After working on this some more, it seems we just can't add a project reference and get server-side classes to come across in the deployed top-level project. Instead, I linked to the "src" folder in SomeSupportProject. This allows things to still compile as normal, but when you deploy your project, all the classes are found without issue.
So this was really an app-engine issue, should have tagged it under there instead.
Thanks!

After working on this some more, it seems we just can't add a project reference and get server-side classes to come across in the deployed top-level project. Instead, I linked to the "src" folder in SomeSupportProject. This allows things to still compile as normal, but when you deploy your project, all the classes are found without issue.
So this was really an app-engine issue, should have tagged it under there instead.
Thanks!

Related

Eclipse Scala IDE: can't open hierarchy for standard library classes

I have exactly the same problem as in this question: Eclipse: Using "Open Declaration" ... in a Scala project
However, I'm using the latest Scala IDE in version 3.0.2 (I have downloaded the Eclipse bundle from the site), and I would assume such basic functionality works by now, and apparently it's me who have something misconfigured.
I have created a new Scala project. Then I open some standard library class/trait/whatever, let's say scala.util.parsing.combinator.JavaTokenParsers. The source is neatly displayed, but when I try to show class hierarchy, I get the message: The resource is not on the build path of a Java project.
Also, searching for references etc. won't work.
I guess it is a matter of properly configuring the build path? Or maybe I should somehow attach Scala library sources to my project? But I can see the source, so aren't they attached already?
Here is the snapshot of my project configuration:
UPDATE:
By playing a bit with setting/resetting build path stuff, I managed to get rid of pop-up warning but the class hierarchy comes up empty and when searching for references I get only hits from my own sources, nothing from standard library.
In another workspace I also tried randomly adding and removing scala-library jars and got it work almost, but the type hierarchy comes up only with super-classes, without any sub-classes (which renders it quite useless). Searching for references works ok though.
Funny thing, I cannot make it work in my original workspace...
Gotta love Eclipse.
Your build path is not configured properly.
If you take a look under Scala Library[...] you have scala-library.jar we can only see one top-level package scala. There should be numerous other packages besides that. (Ruled Out)
I would recommend you follow these steps
Right-click project, build-path, Java-build-path, Libraries and make sure that the correct library is referenced there.
If it is the one you need, Try to remove this library and add it again, then clean and re-fresh the project. Also try this step in a fresh workspace.(something must have messed up this workspace )
Lastly. Goto the path D:\Eclipse For Scala\configuration\org.eclipse.osgi\bundles\286\1\.cp\lib and verify the sizes of the jars there. There should be 6 jars there and the size of scala-library jar should be around 6.8M. If size is smaller, consider re-downloading

Creating Java library file with IntelliJ IDEA

I'm trying to create a library which could be used in other projects. I've written one class with several static methods to do some stuff. I wanted to try it out but I am not able to use the imported JAR file.
I have compiled my code as an artifact and took the JAR file from "out" folder and then copied it to another project. After that I went to "Project structure", tab "Libraries" and I pressed the plus button. I've found the JAR file and selected it, afterwards IDEA asked me to specify dependencies so I did, but when I want to use it in code I am not able to do so. It can't even be imported.
Any ideas why it ignores my library? Thanks!
What should I do in order to create a JAR library with IntelliJ IDEA, that is usable in other projects?
You are running into a very common dependency management problem.
IMO the real answer is to use a build system like Maven, Ant, or Gradle (I'd go Gradle myself). What you are trying to do is manual, hard to reproduce, and brittle.
Every time you make a change you will have to go through manual steps to create a new JAR. And you really don't understand your dependencies.
To go all out with best practices you would be to have real build system that publishes to a continuous integration server, which compiles and runs tests. On successful completion of the tests, the JARs are published to an artifact server (Nexus/Artifactory).
The people you are sharing with would consume the JARs via the build system by declaring dependencies on your JAR.
I figured out what my problem was. When I created the library I was trying to make it simple. Too simple, unfortunately. I had a package with a class in it that was compiled into a JAR. Structure shown below:
foo
|
|_ MyLib.java
However in order to use classes from a created JAR library they have to be placed in packages. That means if I have:
foo
|
|_bar
| |
| |_MyInnerLib.java
|
|_MyOuterLib.java
I am able to import and use methods from MyInnerLib but MyOuterLib isn't reachable nor importable. This was the error I was making.

How to debug the error "Class file needed by X is missing. reference value Y of package Z refers to nonexisting symbol" in Scala?

I am just getting started with Scala since last week and I am having this problem that is driving me crazy.
I have some code that I want to migrate fully from Java to Scala (except libraries) but I have hit a block where I can't now proceed because of this mysterious error.
Let's say that I have a class that I have defined in org.domain.subdomain.MyClass
I have a class that seems to have no compilation problems (as suggested by Intellij) but when I build my project's modules, I would get the following
Class file needed by MyClass is missing
reference value subdomain of package org.domain refers to nonexisting symbol.
I am certain (or almost?) that I have got the classpath right as the projects were compiling before I started rewriting the code in Scala.
I have not been able to get it to compile with the verbose option on. I am using FSC compiler as the project compiler. Intellij does not show anything with verbose option!
What is going on here?
How can I solve this?
I just had this problem, and reimporting the project in IntelliJ worked. I removed .idea directory and .iml file, then reimported the project as maven.
This is an IntelliJ issue - outside maven builds worked fine.
Your classpath is incomplete. The reference to org.domain.subdomain appears in a classfile referenced by one of our sources (as indicated by the error message).
If you are rewriting Java code, you may need add more of your transitive dependencies in each module's classpath. The Scala compiler is more eager when it comes to type-checking and in some situations it needs a larger classpath.
I know, the question is more than one year old, but maybe it helps someone.
I just had the same issue. One of my libraries used an newer version of org.scala-lang:scala-library than the main project. After the library update everything works fine.

What are best practices for using Hibernate's hbm2java?

I am using Hibernate, Maven, and Eclipse (STS build) to build a project. I'm using hbm.xml files to specify my schema. I want to use Hibernate's hbm2java to generate my model classes. I have it working well and generating the kind of code I want.
It runs perfectly from the command line, generating the model code and then building and testing as expected.
However, Eclipse seems unable to handle it. It will periodically "lose its mind" and be unable to resolve very simple imports and classes referenced in my DAO classes, which are hand-coded. The things it can't find are classes like HibernateUtil. Ironically, it appears to not have any trouble finding the model classes.
The unresolved classes are in target/classes/blah-blah folder at the end of the run. So they're apparently getting copied to the right place.
In a "continuous integration" environment, is it best to generate the sources once, commit them to my version control, and then disable code gen? Or is it possible to have the code generated each time, thus ensuring I pick up any database changes without human intervention?
IMHO, entities should be the core of your application, and should be designed, implemented and documented with care. They're supposed to be objects, with methods encapsulating behavior. Having them autogenerated is an absurdity, IMO.
Generating them at the very beginning might be an option to get you started, but once they've been generated, hand-craft them and don't generate them again. Add necessary properties and methods as the schema changes, and refactor existing code.
BTW, I really prefer using annotations for the mapping, because it's less verbose, less error-prone, and all the information is in a single place.
Try this:
From command line traverse to your project directory where the project's pom.xml is present and run:
mvn eclipse:clean eclipse:eclipse
If it says unable to find plugin eclipse then try:
mvn eclipse:install-plugin
First and then try the command above again.
In this way all the maven and project dependencies will be resolved at eclipse level also.
Let me know if this is not what you were looking for.

How to setup Groovy + Eclipse + Junit4?

I am working on a small webapp and I want to use Groovy to write some unit testing for my app. Most of my coding is done on Eclipse and I really want to run all the unit testing with the graphical test runner within Eclipse (I really like the green bar :) )
Sadly, after 4 hours of try-and-error, I'm still not able to setup properly. I tried to use the Eclipse Junit4 test runner to run a Groovy file with method annotated for testing using #Test. But it keeps complaining NoClassDefFoundException
Anyone can help?
Here is content of my groovy file, named simpleTest.groovy
import org.junit.Test
import static org.junit.Assert.assertEquals
class simpleTest{
#Test
void trial(){
assertEquals 6, 3+3
}
}
Anyone can help?
You might want to give the updated plugin a try, see the the recent blog post for more details. The theme of this alpha release is providing an optimized edit/save/compile/test experience, which seems to be your exact use case.
I have this working in my environment so here is a brief summary of what I have:
In the run dialog under JUnit:
Test Tab: The test class, this must have already been compiled by the Groovy plugin.
Classpath: All of the Jar files from my project as well as the Groovy Libraries library
In Window->Preferences->Java->Build Path
Classpath Variables: GROOVY_ECLIPSE_HOME = the location where the Groovy plugin is installed
That does the trick for me.
Unfortunately, the Groovy Eclipse plugin is pretty horrible at giving actual helpful information to let you know what is going wrong with your setup. I'm going to assume you already did the verification to make sure the plugin is actually building your Groovy files (i.e. doing a sample with no dependencies, checking the properly output directory, etc...) After that, it's a lot of really small configuration verification...I've run into problems where the particular "runner" I'm using in Eclipse (i.e. in the Run menu) doesn't have the write class name defined there or for some reason my project didn't get the JUnit library dependency properly inserted into it.
Ultimately, it can be a configuration headache, but long term you'll end up saving some time and gaining some cool functionality if you can knock it out...
I had faced a similar issue and it was the missing package statement that caused me to have problems. Groovy Eclipse plugin did not complain about it but my class was present in a package. I got the noClassDefError when running the file as a JUnit Test.
Adding the package statement to top of class solved this issue.