Netbeans: "main" class in java project - java

I have a problem. I had project in eclipse. I changed my IDE to netbeans.
Now i have problem, because in eclipse i had two source folder:
src/resources/main
src/java
All my jars i have in src/java, so i added src/java to my source in netbeans.
But netbeans cant find class with "main" function in this project when this class is in src/java. Set main class in properties doesnt work.
But when i move it to src/resources/main it works fine.
Is it possible to have main class in src/java? Because i dont want change my structure

There are two folders, and two issues. We will fix them one at a time. Then they will be explained.
make a main directory just under src
move the src/java folder into src/main, the result should be src/main/java
Then to fix your resources folder
move the src/resources/main into src/main renaming it at the same time to src/main/resources.
Now the rationale. This isn't really about Eclipse or Netbeans, it is about Maven. Maven imposes a particular directory naming convention. Maven has put some time and effort into the convention, and it is generally well thought out. More importantly, Maven has been pretty popular, and it's convention is seen as a de-facto standard.
Your Netbeans project isn't properly configured. However, you could solve it one of two ways: you could configure every detail, or you could adapt to the standard expected conventions. Since this isn't just Netbean's conventions, but also Maven's conventions, and most IDEs expect these conventions (or at least accommodate them), it is probably a better idea to follow them than to reconfigure you Netbeans to find sources and resources in nonstandard locations.

You can manually specify the main entry class.
Right click on your Project in the project explorer
Click on properties
Click on Run
Make sure your Main Class is the one you want to be the entry point. (Make sure to use the fully qualified name i.e. mypackage.MyClass)
Click OK.
Run Project :)

Related

How to call method/variable of one eclipse project to another eclipse project?

I have two Projects in eclipse and I simply tried to call method of one project to another but not able to do it even if I set up the whole flow of project setup using properties and even classpath.
I have project CacheProject which contains CachePackage as a package which has CacheMain.java class. This CacheMain class contains a public method getName() which should return me a value of one of the varible there as string.
I have also created jar for the respective CacheProject and added to classpath of another project CacheConsumer1. also I have added project CacheProject to Right click on CacheConsumer1 -> properties -> java build path -> projects.
Still I am neither able to access CacheMain nor getName() method of it:
also I am not able to access anything from the CachePackage:
Please help with it guys if anybody has already worked with something like this. Because none of the earlier answer posted in this stackOverFlow post is working for my scenario.
Actually everything would be easier if you would learn to use Maven or Gradle in order to build your projects and define dependencies. But in Eclipse you can just open both projects, then go to the project properties of project CacheConsumer1 and set project CacheProject as a build path dependency:
This will also lead to CacheConsumer1 automatically being rebuilt as soon as there are changes in CacheProject.
Update: I just noticed in your screenshot that CacheProject has no src folder or any other folder marked as source folder (the folder icon with the little parcel in it). Maybe you just start over, create a new Java project with default Eclipse folder structure and move the class or classes over to that project, creating your Java packages and classes inside the src folder.

refactor method name in eclipse from linked project

I have 6 java projects in my Eclipse IDE (juno). One of them is a dummy project which is not meant to be compiled or deployed, it simple holds common logic for the other projects. The other projects use this common project. I edited the classpath of the other projects to include the common source folder as a linked source.
This works fine, but I'm having troubles when I refactor a class or method name in the common project. The refactoring is not propagated to the non-common classes in the other projects that makes use of the refactored class or method, resulting in compile errors that a certain class or method is not found.
The only way I can deal with this is either:
Don't change the name of anything
Manually change the name in all projects after refactoring
This kind of defeats the purpose of having the common source if a simple edit is not propagated. Is there any way of dealing with this?
Try eliminating the "common source folder" of the classpath of your other projects, and instead set the common project as a project dependency in eclipse. To do that, right-click on the project in the project explorer window, choose "properties", then "build path", then the project tab. Add the common project there. I know this allows things like finding where methods in the library are called from anywhere in the workspace, and I feel sure refactoring will work that way as well.

Trouble understanding the usage of external libraries in a java project/IntelliJ

Now that I've learned a bit more, I can ask a more direct question.
Scenario:
I have a Java project in IntelliJ Idea, with the end goal of making a standalone .jar capable of running on Windows or Linux, either run from the command line with java -jar jarname.jar or simply by being double-clicked like any normal, simple jar.
I've written a handful of classes, located in my src/package/name directory. However, one of my classes requires the use of an external class, i.e. a class not located in my source directory or is not part of Java's default set of .jar's in the JDK.
How do I go about configuring IntelliJ to build the .jar artifact AND include the necessary resources inside of it, with everything needed put in the proper place, so that my class can use the resource by an ordinary import statement? An answer given in example form would be awesome; I've almost figured it out, but there must be just one thing I'm not doing correctly.
Thanks for your time,
Yankee
Sources:
http://tukaani.org/xz/java.html
Viewing the directory structure of that source gives a better idea:
http://git.tukaani.org/?p=xz-java.git;a=tree
One does not simply package Mordor into their jar.
After much experimentation, I found a solution that, while maybe not the right way to do it, definitely works. The key is to:
Define your external library (a .jar in my case) as a module dependency.
Add your external resources as (what IntelliJ calls) an "Extracted Directory."
For the first item, go to File -> Project Structure. Click "Modules" in the Project Settings list on the left. In the list just to the right, you will see a list of modules (whatever they are) which belong to your project. Leave that alone, but make sure that it is highlighted/selected as the current module. What you want is the settings for that module, which will show in the window on the right. Go to the "Dependencies" tab. On the rightmost part of the screen will be a little green plus sign. Click that, choose "Jars or directories" and navigate to your relevant resource you want to bring along with your finished jar.
Note: don't be fooled by the check box that says "Export." Its only purpose is to cause endless pain and suffering as you wonder why the dependency isn't exported along with your jar. Always remember, the export box is trying to get you to click on it. It wants to be ticked :3
Next, add that resource as an extracted directory by going to File -> Project Structure. Then "Artifacts" in the Project Settings list on the left. Click the green plus sign at the top to make a new "Jar" artifact "From modules with dependencies." In the right hand window, under the "Output Layout" tab, click the little green plus sign and choose "Extracted Directory." Once again, browse to your precious jar.
You should now be able to successfully build a jar artifact that contains all the resources it needs to run as you designed it.
Here is a reference section on configuring external libraries as module dependencies - link. Regarding artifacts construction - it's possible to precisely specify its content (including dependency libraries content) - link.
Read How classes are found...
It is OK (expected that you) to ship your program in your own jar, and dependent 'libraries' as separate jars
You are expected to provide a way to run your program with the correct -classpath argument to the java command so that java can find both your jar, and the dependent jars... there are multiple ways to do that... see the link above.
Rolf

Eclipse rename not working completely

Ok I'm completely googled-out.
I have a few java projects in my eclipse workspace (about 25). Most projects use linked source folders.
When I rename a class in Project1, the references to that class in the other projects are not updated. The references within the project are updated just fine. The net result is compilation errors on the next automatic recompile.
I'm using Eclipse 3.5.1, but the same behavior was shown by 3.4.0 and 3.4.2
Any ideas about how to fix this?
My current plan - after googling for 20 minutes - is to write a script to convert the linked source folders to be OS links (I'm on linux) in the project folders themselves. So then I'm no longer using linked source folders.
Like Philippe Faes said, it works fine if your projects are set as a dependency on each other. Make sure that your project's build path are set up that way.
IMHO linked folders are for external files, not for another projects.
Another problem is that if you are just referencing a jar as a dependency (ie, on the project's class path), eclipse will not be able to know that the jar was build based on another project's source thus will not refactor properly.
Try to ask your colleagues for the eclipse project files (.project and .classpath) and edit the files if you need, then check if your refactor still doesn't work. I am guessing that your project is set up differently than your colleagues.
Cross-project renames work just fine if your projects depend on each other.
What exactly do you mean with linked source folders: do you link to the same source folder more than once?
This is a shot in the dark, but make sure that your project is a Java project. I am not sure if other project types (like the generic Project) might not refactor properly. I have never seen this problem before and it has always worked as expected for me...

Eclipse classpath entries only used for tests

In Maven, you can have compile-time dependencies and test dependencies. This is a feature I love, and the M2Eclipse plugin makes this available in Eclipse, too, which is great. So if I add jmock.jar to my project as a test dependency, it will show up on the classpath for JUnit tests, but won't be present when I'm debugging the application itself.
This is exactly what I'd like to achieve now, but without M2Eclipse or Maven. Is there a way to do this in plain Eclipse? (Possibly without installing any plugins.)
You could separate all your tests into another project and add the main project as a dependency (Project->Properties->Java Build Path->Projects->Add...)
Update: To avoid changing the original project structure, your test projects can use linked locations.
Create the test project as normal, you now need to create a linked resource to bring in the src/test/java folder. It is best to create it using a variable so that your projects can retain some platform independence.
To create a new linked folder select New->Folder, input src in the folder name: field then click Advanced>>
Click Link to folder in the file system
Click on Variables... to bring up the Select Path Variable dialogue.
If this is your first time, or you are linking to a new location select New... and give the variable a sensible name and path. If all your projects are located in c:\workspaces\foo** it makes sense to call the variable **WORKSPACE_ROOT and give it that path. If you have some other convention that is fine, but it makes sense to put a comment in the .project file so someone has a chance of figuring out what the correct value should be.
Assuming the values above you can now set a value of WORKSPACE_ROOT/[subject project name]/src on the input field
Once you confirm that you should see the src folder with a little arrow, and if you look in the .project file see something like this:
<linkedResources>
<link>
<name>src</name>
<type>2</type>
<locationURI>WORKSPACE_ROOT/esf-ns-core-rp/src</locationURI>
</link><!--NOTE the WORKSPACE_ROOT variable points to the folder containing the subject project's sandbox-->
</linkedResources>
You can now add the src/test/java folder as a source location as normal.
Note you can also share just the src/test/java folder by changing the config to something like this:
<linkedResources>
<link>
<name>src/test/java</name>
<type>2</type>
<locationURI>WORKSPACE_ROOT/my-project/src/test/java</locationURI>
</link>
</linkedResources>
This gives more control over the config, but you would have to repeat for src/test/resources, src/it/java etc.
You then set all the test dependencies only in the test project.
Very not pretty, but it does work (I've also used this where my test compliance level is different to the main compliance level, e.g. 1.5 for tests, but 1.4 for the target environment).
I'm afraid the answer is that you can't. There are 2 open issues which were postponed from 3.5 related to your problem:
Ability to mark source folder as test sources
[buildpath] Should be able to ignore warnings from certain source folders
Since you use both Eclipse and Maven you can workaround it.
Create a new "Maven Build" run configuration with goal "exec:java" and parameters "exec.mainClass=com.example.Starter". This way the classpath will be calculated by Maven.
Actually if you look in eclipse as to how Maven integrates dependencies it will not make the difference in test or runtime dependencies your test libraries are always accessible.
Maven will keep the difference when packaging the application and when it generates the runtime classpath if maven has control over the execution of that part. When eclipse is concerned Maven simply adds them all without question to the eclipse build path.
Why is it you need to have this separated like so ? What will this help you acheive ?
Eclipse Photon finally added this feature, with m2e support for it.

Categories

Resources