How to compile & run a project-less file in Netbeans? - netbeans

Netbeans, indeed any Java compiler and
executor, needs to know the classpath
to compile and run a file. When you
just open a file in Netbeans, you do
not tell it any of that information.
When you set up a project, that
process tells Netbeans where the Java
resources are.
Yet other IDEs can easily do this. It's not good practice, but it's simple and quick for "fooling around".
Is there any way to compile & run a file, that doesn't belong to a project, in Netbeans?
edit: The options are greyed out if the file doesn't belong to a project.

Your best option with NetBeans is just to create a dummy "try stuff" project that you know will never contain a shipping product but will allow you to experiment without having to create a new project every time.
You could even collect code snippets in different classes this way. If you add a main() method to each experimental class, you have a way to run them all in the IDE just by changing what you have set for the main project class. This is what I do and it works pretty well.

Why not using 'create new project with existing sources' which can be done for Java, Ruby, ... projects?
You can do so and netbeans will create only a nbproject folder within your project's dir and all should be fine.

This solution doesn't work great because projects are also "typed" and I work in 4 languages. I'd hate to have a "rubystuff" project, a "pythonstuff" project, "javastuff", etc, etc...

Related

In NetBeans Java, how do you create a multi-module project?

I want to create a multi-module Java project, in NetBeans 15.
By "module", I mean the Java-9 "modularity" feature, where each module folder has a module-info.java file that specifies its exports or depends or etc.
I know how to code a multi-module application. I just don't know how to do it as a NetBeans project. Yet I'm sure that lots of you StackOverflow readers have been there, done that. How can I do that, too?
Also, I am not using a plug-in, I am not using JavaScript. I am not using Maven, but Ant would be OK.
There is a complete tutorial at https://netbeans.apache.org/tutorials/nbm-projecttype.html, but this shows how to create a NetBeans module, for the NetBeans IDE, so that is the wrong place to look.
Got it! I've succeeded, I built a modular project in NetBeans.
But first, I had to understand how modules work in Java. I kept my coding as low-level as possible. I used only non-IDE file explorer, NotePad++, and Windows Command Prompt. Instead of automating the process with CMD scripts, I stuck with Java's own #argfiles. The strategy was KISS (Keep It Simple Stupid), even though the typing was brutal, I really really had to watch where and what I was typing. And plenty of times, I wished I had the NetBeans "Tools" and "Edit" features. But finally I am ready to turn to NetBeans.
Yes, NetBeans does modules!
Follow these steps:
New project > Java with Ant > Java Modular Project.
Project Name and Location is your choice.
In the "Projects" window, right-click the name,
New Module, and add the module name
In the default package, right-click, and
add your packages
add your classes
code your module-info
another New Module etc etc
eventually you'll get your modules in. NetBeans is good at catching typo's and missing items.
run your multi-module program!
All this procedure is almost as easy as writing a non-module program. The extra layer of modularity should be logical and functional. For my project, I had to think twice about module boundaries and names.
So! It's working. The NetBeans IDE and debugger are working for me.

what is the right way of importing a maven project in Eclipse and run it as a Java project?

I don't understand exactly what is the way of working with maven projects in Eclipse.
The problems I have are often with projects I download from github. If I set the project myself it usually work, so I think I'm doing something different from the majority of people.
I'll try to detail a specific case:
I clone a repo, let's say: https://github.com/spring-guides/tut-spring-boot-oauth2
In Eclipse I import "existing maven project"
The project has a "Maven nature" as indicated by M on the folder icon
I try to run java class with main from Eclipse. First strange thins is that the "Run As" menu doesn't have "Run as a Java application"
I have to configure the configuration manually. Now it runs, but strange things happens, like I can't edit the file as the "content assist" throw errors instead of giving the normal assists.
I notice that the project has no "source folder". So my first instinct is to add a Java nature or select src as source folder
So I add Java nature to the project. This is a disaster. It can compile anymore as it can find packages. All classes have errors. I try to play around setting source folders on /src or /src/main/java. Sometimes I fix the errors but I can't run (and now I have run as Java application) but when I run it can't load the class
So in the end, I'm a bit confused and I don't know if I explained clearly what I'm doing.
I think I would like to know in a simple way how people are doing it, rather than trying to correct my steps as I'm probably creating a mess myself.
Any help or suggestion welcome.
I'm using the last version of eclipse. I don't know which other tool's versions are relevant.
P.S. I also refresh,restart,clean rebuild the project often after touching things...but it doesn't get better
Maven is a build (management) tool. Simply spoken, its task is to create a JAR that can be used as a dependency/library by other projects or when running java -jar ....
Running a project's code isn't part of it (apart from unit and integration tests code and by using non-default plugins for special situations). Running code is part of Eclipse (or any other IDE) with its Run Configurations.

How to import an eclipse based project from GitHub into Intellij properly

I want to import a maven project from GitHub into intellij which in of itself I know how to do however, the project this time was created initially also as an eclipse project and I want to be able to work on it in idea while my team members work on it in eclipse without causing conflicts due to differing project file structures. How can this be done?
When checking it out in Intellij it does ask me if I want to create a project from it and select yes but next, it asks whether to create the project from the existing sources vs from external model. From the external model option it allows me to pick eclipse or maven but not both. Do I just create from existing sources? Which is my best option to do this without screwing it up for the others when I commit and push my changes?
I understand that the easy answer might be: "just use eclipse" however, I think the answer on how to properly do this could be useful later. Not only me but also for others who want to work with the IDE they are most familiar and productive with and not mess with the workflow.
Any help would be appreciated
When importing an Eclipse project into IntelliJ, you have the option to:
Create module files near .classpath files
Keep project and module files in
The idea is to keep your *.iml file concurrently with your Eclipse .project/.classpath (and you can keep them in sync).
That way, you can open the project in both IDE.

How to get Eclipse to create bin/main and bin/test

I want my Ant build to take all Java sources from src/main/*, compile them, and place them inside bin/main/. I also want it to compile src/test/* sources to bin/test/. I wan this behavior because I want to package the binaries into different JARs, and if they all just go to a single bin/ directory it will be impossible* (extremely difficult!) to know which class files belong where.
When I go to configure my build path and then click the Source tab I see an area toward the bottom where it reads Default output folder: and then allows you to browser for its location.
I'm wondering how to create bin/main and bin/test in an existing project without "breaking" Eclipse (it happens). I'm also wondering that if I just have my Ant build make and delete those directories during the clean-n-build process, that Eclipse might not care what the default output is set to. But I can't find any documentation either way.
Thanks in advance for any help here.
In Eclipse, you can only have one output folder per project for your compiled Java files. So you cannot get Eclipse to do the split you want (i.e. compile src/main to bin/main and src/test to bin/test).
You can, if you want, create two Eclipse projects, one main project and one test project, where the test project depends on (and tests) the main project. However, in that case, each project should be in its own directory structure, which is not what you are asking for. But this is a common approach.
Another way, which I would recommend, would be to not mix Ant compilation and Eclipse's compilation. Make the Ant script the way you describe (i. e. compile the main and test directories separately and create two separate jar files). Change the Eclipse compile directory to something different, for instance bin/eclipse. Use the Ant script when making official builds or building for release. Use Eclipse's building only for development/debugging. This way, your two build systems will not get in each other's way and confuse each other.
Hope this answers your question and I understood it correctly. Good luck!

How do I run an arbitrary java file with Eclipse?

I need to run some example java code. I like using eclipse because it shows the methods, properties, constructors and so on. The problem I'd like to solve is that unless I create a new project I can't compile and run the example code I'm using! Is there a way around this?
No, there is no way around this. A Java file is very rarely a self-contained program with no dependencies at all. Most of the time, a program is composed of many collaborating classes depending on external libraries.
If you just want to test a self-contained Java program consisting of just one java file, then create a "playground" eclipse project, drop this file inside this project, and run it. Use the same playground project for all the other self-contained java programs if you want: you may have several main classes inside a single eclipse project.