Eclipse maven project package disorder - eclipse

Maven project is usually organized as src/main/java/somepackage, but from time to time my eclipse would recognize src/main/java as part of package too. How to fix this?
In the following picture, common and helloworld are both maven projects, where common is organized correctly but helloworld is not.

The src directory is the default directory where Eclipse stores the source code, while in Maven is src/main/java. Better check if the .classpath file has not been corrupted or externally modified. It should contain something like this:
<classpathentry kind="src" output="target/classes" path="src/main/java">

The code line
package main.java.helloworld;
in the helloworld project results in this package hierarchy:
main
`-+ java
`-+ helloworld
Is that really what you mean?
In a maven project, you have this directory structure:
src
`-+ main
| `-+ java
| | `-+ <here your packages and classes>
| '-+ resources
|
'-+ test
| `-+ java
| | `-+ <here your test packages and classes>
| '-+ resources
src/main/java (production code) and src/test/java (tests only) are the root of your package hierarchy. If you create the nested packages com.enterprise within test/java and create a class Test within enterprise, then you have to insert this package declaration in Test.java:
package com.enterprise;
Take a look at your directory and package structure in the helloworld project.

Related

Compiling scala file placed under resource folder and access it in test class

I have a project setup as:
Project
|__src
| |_main
| | |_src_file_1.scala
| | |_src_file_2.scala
| |
| |_resources
| |_resource_file_1.scala
|
|__test
|_test_file_1.scala
Source files inside main (src_file_1.scala, src_file_2.scala) are all compiled but resource file which is inside resources folder (resource_file_1.scala) is not compiled. It contains some scala code under a class which needs to be tested.
Is there a way to compile this class and access it inside test_file_1.scala which is a test file and tests all the other source files.
Note: I would like to have a way to read the source code, compile it inside test class using scala compiler and then use the compiled code for the testing purpose.
Is there a specific reason why you want to have your project structure like that? There is a convention for sbt as outlined here https://www.scala-sbt.org/1.x/docs/Directories.html
src/
main/
resources/
<files to include in main jar here>
scala/
<main Scala sources>
java/
<main Java sources>
test/
resources
<files to include in test jar here>
scala/
<test Scala sources>
java/
<test Java sources>
Everything inside scala will be compiled.

The package com.faizan.org is accessible from more than one module: ProjectA, ProjectB using JDK 9+ during build in Eclipse 2019-12

I have 2 projects, say ProjectA and ProjectB, both containing package com.faizan.org. ProjectA is added in the modulepath of ProjectB.
<classpathentry combineaccessrules="false" kind="src" path="/ProjectA">
<attributes>
<attribute name="module" value="true"/>
</attributes>
</classpathentry>
Now I am writing a new class in ProjectB that needs to import a class from com.faizan.org of ProjectA, but I get an error The package com.faizan.org is accessible from more than one module: ProjectA, ProjectB in eclipse 2019-12 using openJdk 12 and compiler compliance also set to 12.
How can I add external projects containing same package name to another project without classpath conflicts?
Also, in some cases unable to access methods of super class.
Simple answer: you cannot.
When you setup your Eclipse projects as Java modules, then the rules of the module system JPMS forbid that any module has access to the same package from two modules (each package must be "uniquely visible").
Next, you should revisit why you need to have the same package in both projects? If it is for whitebox testing, then please consider moving the tests to the same project, but in a separate source folder marked as containing tests. Then Eclipse will do all the necessary wiring behind the scenes, so that the tests are part of the module and not part of the module at the same time.
If it's not for the sake of whitebox testing, and you do want to adopt JPMS, then you are left with 2.5 options:
Move all code that shares a package into the same project / module.
Change the package structure to avoid the split package.
(Use a tricky set of JPMS options including --patch-module and likely more to let JPMS view the separate projects as one module -- while possible I would consider this as "successful migration")

Eclipse - Wrong project name when importing maven project

I'm using eclipse mars and I'm working on a multi modules maven 2 project.
Since now it was always working fine when importing the project into eclispe.
My project structure is like
myproject
|-common
|-dao
|- ...
In each pom the artifact-id is
<artifactId>myproject-common</artifactId>
<artifactId>myproject-dao</artifactId>
...
What I always did till now
retrieve the project from svn
mvn clean install
mvn eclispe:eclipse
in eclipse import > existing project > browse to myproject and it displays the good project names : myproject-common, myproject-dao, ...
But since today the last step gives me the folder name in place of the artifact name : common, dao, ...
And after import, lot of errors saying eg. that Project 'dao' is missing required Java Project: 'myproject-common'
I don't understand. Same version of eclispe,maven and java than few months ago.
And after mvn eclipse:eclispe, the .project file is containing the good name
<projectDescription>
<name>myproject-common</name>
How can I solve that ?
I solved it by changing:
import > existing project
to:
Import -> Maven -> Existing Maven Projects

running RabbitMq in CMD

I'm currently doing the tutorials that can be found on the rabbitMQ webiste and I've run into problems when running the programs in command prompt (Windows7).
Now I have the client libraries working perfectly in Eclipse i.e. I'm able to send messages between a producer and consumer etc. But if I try to run this program in command prompt I get this:
Worker.java:1: error: package com.rabbitmq.client does not exist
import com.rabbitmq.client.Channel;
^
Worker.java:2: error: package com.rabbitmq.client does not exist
import com.rabbitmq.client.Connection;
^
Worker.java:3: error: package com.rabbitmq.client does not exist
import com.rabbitmq.client.ConnectionFactory;
^
etc...... etc..... (this list goes on)....
Now I have added the necessary library (.jar files) locations to the classpath! and compiling in the command prompt using:
javac -cp rabbitmq-client.jar Worker.java
but i'm still getting these issues. If anyone can help I would be most grateful.
Cheers!
I had a similar problem when trying to get RabbitMQ's sample rabbitmq-java-client running (Netbeans on a linux machine). Even though the code in Netbeans said the packages were there, running mnv install or javac would produce tons of errors like:
Send.java:11: error: package com.rabbitmq.client does not exist import
com.rabbitmq.client.ConnectionFactory;
I solved it fixing the structure of the directories. They should look like this:
my-app
|-- pom.xml
`-- src
|-- main
| `-- java
| `-- com
| `-- mycompany
| `-- app
| `-- App.java
`-- test
`-- java
`-- com
`-- mycompany
`-- app
`-- AppTest.java
Please see: http://maven.apache.org/guides/getting-started/index.html#How_do_I_make_my_first_Maven_project
I think that matches what Maven is looking for.
Also, I rebuilt the pom by hand. The pom in the sample rabbitmq-java-client lacks all this information:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
It just has
<?xml version="1.0"?>
See:
https://github.com/rabbitmq/rabbitmq-java-client/blob/master/pom.xml
Actually, I'm wondering why the poster is doing this in Eclipse.
Now I'm guessing here, but his Worker.java does not need to be edited in an IED. I wonder if we both made the same mistake.
They don't need to be. Just put them in their own directory with these files
commons-cli-1.1.jar
commons-io-1.2.jar
rabbitmq-client.jar
Then
javac -cp rabbitmq-client.jar Worker.java
Open RabbitMQ server (http://localhost:15672/) then
java -cp .:commons-io-1.2.jar:commons-cli-1.1.jar:rabbitmq-client.jar Send
next
java -cp .:commons-io-1.2.jar:commons-cli-1.1.jar:rabbitmq-client.jar Recv
This is from: https://www.rabbitmq.com/tutorials/tutorial-one-java.html
The Java client library
RabbitMQ speaks AMQP, which is an open, general-purpose protocol for
messaging. There are a number of clients for AMQP in many different
languages. We'll use the Java client provided by RabbitMQ.
Download the client library package, and check its signature as
described. Unzip it into your working directory and grab the JAR files
from the unzipped directory:
$ unzip rabbitmq-java-client-bin-.zip $ cp
rabbitmq-java-client-bin-/*.jar ./ (The RabbitMQ Java client is also
in the central Maven repository, with the groupId com.rabbitmq and the
artifactId amqp-client.)
More details and download the Java client library here: http://www.rabbitmq.com/java-client.html

How do you ensure a Utility Projects library dependency gets packaged in the final EAR in Eclipse Galileo?

I have a 'Utilty Project', and an 'EAR Project' that includes that 'Utility Project'. All the classes from the 'Utility Project' end up being packaged as a JAR and placed within the 'lib' directory of the exported EAR, for example:
EAR.ear
META-INF
MANIFEST.MF
lib
utility.jar (which expands to):
META-INF
MANIFEST.MF
com
acme
Foo.class
Bar.class
However, the 'Utility Project' relies on a library (freemarker.jar) that has been added to the build path using 'Properties > Java Build Path > Libraries'. All I want to do is to get freemarker.jar added to the EAR as follows:
EAR.ear
META-INF
MANIFEST.MF
lib
**freemarker.jar**
utility.jar (which expands to):
META-INF
MANIFEST.MF
com
acme
Foo.class
Bar.class
By searching around within Eclipse I've found 4 potential avenues for achieving this, none of which have worked. If someone can just cut to the chase and tell me what I should actually do, that would be great. But just in case, I'll iterate them here:
From the 'Utility Project' Properties:
If I click 'Java Build Path > Order and Export' and select 'freemarker.jar' for export, the jar does not end up in the EAR file at all.
If I click 'Java EE Module Dependencies' and select the 'freemarker.jar' library as a dependency, it says:
This JAR is a bundled library of an
EAR project and is supposed to be
packed in the EAR's library directory.
It conflicts with the manifest class
path dependency you are trying to
create. If you create this dependency,
the JAR will be packed in the root
(not library) directory of the EAR.
Are you sure you want to proceed?
From the 'EAR Project' Properties:
If I click 'Java EE Module Dependencies > Add JARs...' and navigate to the 'freemarker.jar', and make it a dependency, it gets added to the root of the EAR:
/freemarker.jar
If I do the same as above, but check the 'In Lib Dir' checkbox, it gets added into the lib folder, but contained within another lib folder:
/lib/lib/freemarker.jar
Thanks.
I've managed to solve this problem. In the Problems window I was getting the following warning:
Classpath entry /utility-project/lib/freemarker.jar will not be exported or published.
Runtime ClassNotFoundExceptions may result.
What I tried, at a whim, was to use the Ctrl-1 key combination I've been using to get quick fix solutions for my source code. It turns out this also provides quick-fix solutions for for the given errors and warnings. I chose the first of the two options:
Mark the associated raw classpath entry as a publish/export dependency.
and my problem disappeared!
Bizarrely, all this seems to have done is to cause the 'freemarker.jar' lib to be selected in the 'Java EE Module Dependencies' properties dialog, which I was doing myself anyway. This may be an Eclispe bug!
#MonoThreaded: After using the quick fix > Mark the associated raw classpath entry as published/export dependency, if you right-click on the project > properties > depolyment assembly you will notice that eclipse has added a new entry, something like source: /freemaker.jar, deploy path: ../lib. You can do that too, by selecting Add > Java Build Path Entry.