Java project structure best practice in Eclipse - java

I am using Eclipse to do some Java programming. I am working with an agent-based modelling library, MASON, and GeoMASON which is an extension that adds support for shapefiles etc. I've been having a lot of problems trying to get the included example models to run and I think it's because my file structure is different to what is expected and/or I have the JAR/data files in the wrong locations.
What is the best file/folder setup when working in Java/working with Eclipse and referencing other libraries and running included example models?
My current model, EngD_ABM_MK0, is in the following location with the following supporting files/folders:
/GoogleDrive/ABM/engd/EngD_ABM_MK0/src/engd_abm/EngDModelBuilder.java
/GoogleDrive/ABM/engd/EngD_ABM_MK0/bin/engd_abm/EngDModelBuilder.class
/GoogleDrive/ABM/engd/EngD_ABM_MK0/data/roads.shp
/GoogleDrive/ABM/engd/EngD_ABM_MK0/lib/mason.19.jar
...
The MASON files are in the following location:
/GoogleDrive/ABM/MASON/sim/app/antsforage/AntsForage.java
/GoogleDrive/ABM/MASON/jar/mason.19.jar
...
The GeoMASON files are in the following location:
/GoogleDrive/ABM/geomason/sim/app/geo/cityMigration/CityMigrationModel.java
/GoogleDrive/ABM/geomason/sim/app/geo/cityMigration/cityMigrationData/road_nodes.txt
...
I also have a folder full of all the JAR files I need in the following location:
/GoogleDrive/ABM/lib/geomason.1.5.jar
/GoogleDrive/ABM/lib/mason.19.jar
/GoogleDrive/ABM/lib/j3dcore.jar
...
In Eclipse I have EngD_ABM_MK0, MASON and GeoMASON as Java Projects (see screenshot). The EngD_ABM_MK0 project references MASON and GeoMASON in the 'Projects' Java Build Path and all the necessary JAR files have been added in the 'Libraries' Java Build Path (see screenshot).
Any advice would be appreciated.

Related

Maven equivalent in .NET C#

Let's assume there are two Maven Java projects, A and B. A has a dependency on B. B is placed in remote Maven repository and also on GitHub.
In IntelliJ Idea IDE, I will open project A and also B (B is cloned from GitHub) in two separate windows.
Now, B has a class named Car. When I 'Ctrl+Left mouse button click' on class Car in project A, IDE switches me automatically to the source code in opened project B. Therefore I can comfortably work together on A and B.
How can I achieve same behaviour with .NET C# and Visual Studio?
The rough equivalent to Maven in the .NET ecosystem is NuGet. NuGet files can be created using the IDE or command-line tools such as dotnet pack or nuget pack. A NuGet file is just a regular .zip file with the .nupkg extension. These files can be hosted on http://www.nuget.org for the general public to consume, on a site such as http://www.myget.org, or on a private hosted NuGet server.
The NuGet tools also have the ability to create debug symbol packages that contain the source code files. The debug symbol packages can be hosted on public or private symbol servers and used in Visual Studio to step through the code, with the configuration options enabled in Visual Studio.
If you open project A in Visual Studio and automatic package restore is enabled and that project has a package reference to project B, when you build the project it will automatically download the NuGet file for project B, unpack it to your local NuGet cache, and use the assembly for project B in your project.
If Visual Studio is configured correctly to find a debug symbols package corresponding to the exact version of project B, it will allow the debugger to step through the code of project B.
AFAIK, opening the code file of project B and then setting a break point is not possible (someone leave a comment if this is wrong), you need to set a breakpoint in project A and then when you step into the line that calls/instantiates the Car class, Visual Studio will open up the code file so you can step through it.
Update 2023-01-15
Note that the above symbols documentation still works, but there is now a better way. If the author of a NuGet package configures Source Link and the code exists in a Git repository, an IDE can enable source stepping and the code files can be downloaded directly from the repository into the IDE's debugger. This means it is no longer necessary to package source files in the symbols package. This feature uses a Git commit hash to ensure that the source code matches the binary even if you upgrade the NuGet package later.
The symbols have also been updated to allow a .snupkg file by including <PropertyGroup><IncludeSymbols>true</IncludeSymbols><SymbolPackageFormat>snupkg</SymbolPackageFormat></PropertyGroup> in the project file. This new format can be uploaded directly to NuGet instead of having to use a 3rd party symbol or self hosted server for debugging.
Well, actually it is possible. But some MSBuild modification is necessary.
Preconditions:
use the new MSBuild format that was shiped with VS 2017
Step by step:
create a solution and add both projects
I assume project A has a package reference to project B and the nuget package names are the same as the project names
in the root directory of both projects, which is probably not part of any source control system, create a file with the name Directory.Build.targets with the following content:
true
<PropertyGroup>
<SolutionFileContent>$([System.IO.File]::ReadAllText($(SolutionPath)))</SolutionFileContent>
<SmartSolutionDir>$([System.IO.Path]::GetDirectoryName( $(SolutionPath) ))</SmartSolutionDir>
<RegexPattern>(?<="[PackageName]", ")(.*)(?=", ")</RegexPattern>
</PropertyGroup>
<ItemGroup>
<!-- Keep the identity of the packagereference -->
<SmartPackageReference Include="#(PackageReference)">
<PackageName>%(Identity)</PackageName>
<InSolution>$(SolutionFileContent.Contains('\%(Identity).csproj'))</InSolution>
</SmartPackageReference>
<!-- Filter them by mapping them to another itemGroup using the WithMetadataValue item function -->
<PackageInSolution Include="#(SmartPackageReference -> WithMetadataValue('InSolution', True) )">
<Pattern>$(RegexPattern.Replace('[PackageName]','%(PackageName)') )</Pattern>
<SmartPath>$([System.Text.RegularExpressions.Regex]::Match( '$(SolutionFileContent)', '%(Pattern)' ))</SmartPath>
</PackageInSolution>
<ProjectReference Include="#(PackageInSolution -> '$(SmartSolutionDir)\%(SmartPath)' )"/>
<!-- Remove the package references that are now referenced as projects -->
<PackageReference Remove="#(PackageInSolution -> '%(PackageName)' )"/>
</ItemGroup>
</When>
What happens here, is that all package references are automatically dynamically replaced with project references, when they are contained in the currently opened solution.
For further information, i have posted that solution also in a MSBuild github issue:
https://github.com/dotnet/sdk/issues/1151

How to generate protobuf 3.0 jar java?

I am quiet familiar with protobuf 2.5. I was trying to use protobuf3.0. It seems that the jar is to be generated form the source code available online. But when i import the source code into eclipse and try to create a jar, i can see many errors in the following files
/protobuf3/src/com/google/protobuf/Descriptors.java,
/protobuf3/src/com/google/protobuf/DynamicMessage.java
/protobuf3/src/com/google/protobuf/ExtensionRegistry.java
/protobuf3/src/com/google/protobuf/MessageReflection.java
/protobuf3/src/com/google/protobuf/TextFormat.java
/protobuf3/src/com/google/protobuf/UnsafeUtil.java
This is one of the errors "the import com.google.protobuf.DescriptorProtos cannot be resolved".
Is this the right way to generate the jar (or) is it available anywhere (or) is the full source code available anywhere.
Any help would be appreciated.
Thanks.
This is where i downloaded the source code protobuf3.0-source code
You can find a compiled (JAR) version of Protobuf to download here:
http://search.maven.org/remotecontent?filepath=com/google/protobuf/protobuf-java/3.0.0/protobuf-java-3.0.0.jar
I recommend you look at a build manager such as https://maven.apache.org/ to automatically handle fetching dependencies (libraries) for you.

Build OpenCV with contrib modules and Java wrapper

I'm trying to build OpenCV on my Windows 7 machine. To include the contrib modules I have added the OPENCV_EXTRA_MODULES_PATH in CMake-gui. The opencv-300.jar and opencv-300.dll have been created but I can not find the Java classes to use the extra modules. Am I missing an option in the make configuration? Is it possible to use these extra modules from Java?
i've the same problem and i resolved in this way. I imagine that you had downloaded from contrib repo the specific version match with the opencv version if you want to build. So go in directory and enter, for example, face module directory; in this directory there is a file called CMakeLists.txt that you have to edit. This file should be like this:
set(the_description "Face recognition etc")
ocv_define_module(face opencv_core opencv_imgproc opencv_objdetect WRAP python)
# NOTE: objdetect module is needed for one of the samples
If you want to have the org.opencv.face package in your opencv-3xx.jar library you have to modify the 2nd line of the file in this way:
ocv_define_module(face opencv_core opencv_imgproc opencv_objdetect WRAP python java)
Then you have to compile opencv as depicted in the Readme.md of the contrib repo https://github.com/itseez/opencv_contrib
Obviously the same thing is valid for all the contrib modules if you want to add to your opencv-3xx.jar library.
I hope that this solution works for you, bye!

Oracle Rules - Imported Java Fact causes UndefinedException when deployed

In JDeveloper 12c, I have created a BPM application/project. Project SOA Settings are Composite With Business Rule.
The project has a Mediator and is exposed as a REST service. I am deploying using the IntegratedWebLogicServer
The XML Facts are rather generic including lists of fields, sub-lists, and sub-sub-lists, etc. This makes navigating the inputs more challenging.
I created a Java class with some static methods that more easily retrieve the data based on an expression. I created a jar with that class and then imported the class into the Java Facts. I then created rules that call the static methods on that Java Fact.
The rules validate just fine and I can now successfully run the rules from the "Test" tab of the rules. I get expected results. However, when I deploy the project it seems like the imported jar is not deployed along with it since I get the following exception:
oracle.rules.rl.exceptions.UndefinedException: The symbol "mypackage.MyUtil" is undefined.
at line 22 column 14 in /Ruleset(main)
I thought that importing the class in the Java Facts would cause my custom jar or at least the class that I imported to be deployed. But it seems that this may not be the case. Am I missing something? Is there somewhere in my project that I can place the jar to cause it to be deployed?
I tried adding the jar to the Libraries and Classpath entries of the project. I also tried adding it to a user library located in the project and adding that to the Libraries and Classpath list. But both had no effect.
I am new to JDeveloper and Oracle BPM, so I'm hoping there's something simple I'm missing here.
The solution was to add the jar to this directory:
MyBpmApplication\MyBpmProject\SOA\SCA-INF\lib

problems building the protobuf example apps

I'm new to protobufs and was trying to learn more about using them. I've downloaded the protobuf packaged from here. There is a README.txt file inside the examples folder of the archive which gives instructions on how to build 2 example applications. However when I follow those instructions for building the java application:
make java
I get a lot of errors followed by:
100 errors
make: * [javac_middleman] Error 1
All of the 100 errors seem to be classpath related, as this is a typical example:
com/example/tutorial/AddressBookProtos.java:37: error: package com.google.protobuf does not exist
Any ideas about how to get passed this?
The problem is that for some reason protobuf jar is not added to the classpath during compilation. To fix it you should open examples/Makefile and add -cp protobuf-java-2.4.1.jar at the end of java complilation line javac AddPerson.java ListPeople.java com/example/tutorial/AddressBookProtos.java.
P.S. If you built you protobufs with maven the jar is located at ~/.m2/repository/com/google/protobuf/protobuf-java/2.4.1/protobuf-java-2.4.1.jar (version of the jar might be different)

Categories