How do I run an arbitrary java file with Eclipse? - 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.

Related

Netbeans create Java source files in a C/C++ project

I have recently started using Netbeans C/C++. I see that the new file wizard doesn't show any buttons to create java files, all of them disabled. I can create java files in a Java project however.
If I create a java file manually and open it inside the ide, it provides syntax-highlighting. But the IDE doesn not show any wizards that will enable me to create new java files inside a c/c++ project.
How can I enable this ?
You can't create Java files in a C / C++ project (and vice versa) . You have to use either Java or C(++).
I don't see a reason why you should mix them, but better you develop your Java code in a Java Project and same for C/C++. However, you have to move manually.
Btw. please explain your problem more detailed, it's hard to understand why you put two (or 3) totally different things together.
An observation, rather than a solution...
We need to support mixed language projects because the dependancies between artefacts-by-language can go in both directions.
For example: Java classes are used to generate JNI headers (javah) for C/C++, while C/C++ headers/libraries are used to generate wrapper classes (swig) for Java.
This represents a bidirectional dependancy between single-language projects, but not between artefacts in a single mixed language project - i.e. the build can be described by a DAG and thus satisfied by make.

Eclipse: script compiler as part of a project

This question is not limited to lex and yacc, but how can I add a custom script compiler as part of a project? For example, I have the following files in the project:
grammar.y
grammar.l
test.script
The binary 'script_compiler' will be generated using grammar.y and grammar.l compiled by lex, yacc and g++. And then I want to use that generated script_compiler to compile test.script to generate CompiledScript.java. This file should be compiled along with the rest of the java files in the project. This setting is possible with XCode or make, but is it also possible with Eclipse alone? If not, how about together with Maven plugin?
(I might setup the script compiler as a separate project, but it would be nice if they can be put in the same project so that changes to the grammar files can be applied immediately)
Thanks in advance for your help!
You can add a custom "Builder" from the project properties dialog. This can be an ant script (with an optional target) or any other script or executable.
There are also maven plugins for ant and other scripting languages
If you just want to run an external program in Maven this is what you want: http://mojo.codehaus.org/exec-maven-plugin/ -- you can then run Maven targets from your IDE or command line and it should do the right thing either way.
To integrate with the normal compilation bind the plugin to the "generate-sources" phase and add the location where the Java files are generated to the "sourceRoot" option of the exec plugin. That way the compiler will pick them up.
Ideally you generate the code into a folder "target/generated-sources/MY_SCRIPT_NAME". That is the standard location for generated sources in the Maven world and e.g. IntelliJ IDEA will pick up source files inside of that location. Note that this doesn't work if the files are directly in "target/generated-sources".
The other option is to write your own Maven plugin, which is actually quite easy as well. See e.g. https://github.com/peterbecker/maven-code-generator

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!

Creating ANT file for use in Eclipse and CruiseControl

I have a project in Eclipse that currently has no ANT build file, but is about to have one created (to facilitate building jars). I'd like to write the build file so that it would work for both. I was just going to write the full build in ANT but I noticed this line in an IBM Help doc.
The Java builder runs the internal
Eclipse Java compiler which in turn is
responsible for indexing your source
so that searching, refactoring and
many other features are available.
Thus it is not possible to replace the
internal Eclipse Java compiler by
using a project builder. You can
disable the Java builder and you can
control when the Java Builder runs
with respect to the project builders
that you define.
Do I need to write the ANT file so that it conditionally compiles (javac) only outside Eclipse? Are there other things I need to do to make ANT and Eclipse play nicely together?
There are maybe more sophisticated approaches, but here's what works for me. Just set up your Java project in Eclipse to output classes to the same dir as your Ant buildfile does. Let Eclipse compile your project using normal Java compiler.
During typical development, you will probably be changing Java source files and wanting to see that they compile. The Eclipse Java compiler will take care of this. Less frequently you will need to rebuild jars. Ant will take care of that. You can kick off the Ant build when you need to from the command line or from Eclipse.

How to compile & run a project-less file in 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...