Why does eclipse create a class file in bin directory as soon as I create a java file in it? - eclipse

As soon as I create a java file in eclipse it creates the corresponding class file in the bin directory which I assume is because eclipse compiles any java file as soon as it is created?Am I right?And later does it compile any java project again when I run it?

If you have Project > Build Automatically checked then Eclipse compiles your Java code as you go.
The code is not compiled again when you Run the program.

You are right for the first part (eg: a *.class in the bin directory).
Eclipse does incremental compilation (eg: almost like compile as you type).
For the second part, I don't understand your question.

It will happen in case Build Automatically option is checked (Menu-->Project-->Build Automatically). If you uncheck that option .class file will not be created when you save java file.

Related

Eclipse Maven Project import

Using Java8, Eclipse Mars 4.5
I have a simple Maven project with a pom and a single java file, in a folder. I do the File -> Import -> Existing Maven Project -> Point to my folder. Eclipse does its thing sucessfully creates the project (no errors). But with somewhat of a complex folder structure that I am able to edit (with some difficulty). My project file structure is:
Project
/classes
/pkg
/.class
/src
/pkg
/.java
I originally see my java file (but not in the /src), I see all my dependent jars. After some doing (open/new etc) I am able finally compile my java class- I see all the generated class files.
But I am unable to run it. I do have my main class specified in the eclipse run configuration.
I don't have the Run As enabled.
I try run java application, get error:
Could not find or load main class 'myclass' (I do have the main method)
If I try Maven build (M2 the red icon), initially I got 'no goal specified' error, added <defaultGoal>install</defaultGoal> to the pom. I do get BUILD SUCCESS
But still no run? What am I missing.
I went the maven project route to simplify, but
Thanks
This problem was finally solved by conforming (submitting) to the default file structure of the eclipse/maven project- <classpathentry kind="src" output="target/classes" path="src/main/java">. I re-created the file structure (as this) put/copied my .java and .classes in those directories - it ran fine finally.
I could see this problem when I opened the .classpath file - it had the original /classes and /src directory and not he one I had edited in eclipse (as I indicated in my Question). Even though I was actually successfully compiling into my /classes.
I would certainly not expect this behavior, there is clear edit option for /src and /classes and it should take effect- but maybe not.
Another observation to add- when I look at my run configuration - I see the correct class but eclipse does not show the location. So one would never know.
So long story short it finally worked after much frustration and effort and loss of a couple of days (And I just had exactly one java file to compile and run - wondering how much pain would be if I had a few more).
I am not a regular eclipse user (any longer) so some of it would be getting used to etc - but some of these quarks need to be taken care of (It is the No1 tool)

Creat JAR from .jardesc in eclipse

I have a question with respect to compiling java codes and building a jar file on Eclipse platform. I would like to know if the Create JAR option on eclipse from a .jardesc file will still create a JAR file inspite of errors in the compiled Java codes? If yes, how can i enable this option in my Ant build xml?
If you edit your .jardesc in Eclipse (Open With – JAR Export Wizard), you can modify the behavior regarding errors/warnings on page two of the wizard:
.
This apparently toggles the attributes exportErrors and exportWarnings, respectively, at XPath /jardesc/options in the .jardesc file.
As far as the behavior of a corresponding Ant build file is concerned, you should probably take a look at the failonerror parameter of the Javac Task.

How to make an executable jar in net beans (JDK 7)?

I have a project which i need to be made as an executable jar file. I used clean and build option in project, right-click menu and it built normally. The problem is, I cant run the executable jar file in project's dist folder (giving an error saying it could not find the main class, but believe me, I have given the main class correctly). I tried this using an example project (Just a Jframe) and gave me the same error. It can be solved by lowering the JDK version into JDK 6 (current version is JDK 7)but for that i have to do major changes in my actual project (such as replacing all switches into for loops and replacing all multi catches in to single catches...etc). is there any way to make executable jar without changing JDK version?
My netbeans version is 8.0.2
Assuming you have a "private static void main" in you main class.
If you open (not run) the Jar file in the dist folder.
Open the MANIFEST.MF file in /META-INF folder.
Do you see something like Main-Class: com.satra.satra_finacials.view.main.MainFrame ?
If not you can define your main class in Menu File -> Project Properties (Satra_Financials) -> Category Run -> Main Class TextField

Groovy Eclipse can't launch junit tests

In Eclipse with groovy plugin, I try to launch a test #Test public void testToLaunch() {...} but I have the error:
The input type of the launch configuration does not exist
What input type is in the context of launch configuration? (can't find such an entry in launch configuration window)
Note: I try sts 2.8.1 and e3.7
This happens normally when the folder in which test case is present is not a source folder, please check this post as well.
Hope that helps!
This can also happen if there is a problem with the groovy class. A few things to check:
1) Ensure that the class name exactly matches the filename (filename = MyTest.groovy)
package com.mypackage;
import groovy.util.GroovyTestCase;
class MyTest extends GroovyTestCase {}
2) Ensure that the package defined in the file matches the package the file is actually in.
In Eclipse you can do
Right click -> properties -> Java build path
Notice test folder is not available in sources. Add it.
Add folder -> Select test -> OK
Now rerun you unit test cases.
This happened to me, and I just restarted Eclipse (GGTS) and everything was fine again.
I had a spelling mistake which lead to that error message. My test class file name was named JUnit5Test.java (with upper U) and the class itself was named Junit5Test (with lower u).
I was using Spring Tool Suite 4 (4.8.0.RELEASE).
This also happened to me. But these tests are written in Groovy. The problem I encountered has to do with how the IDE (Eclipse Kepler, Java EE) first opens a Groovy project after executing "mvn eclipse:eclipse".
The Build Paths do not reference the Groovy source files correctly.
To resolve, I:
Right-click on the project, select "Build Path" > "Configure Build Path..."
Select "Source" tab
For test and src folders (.../src/main/groovy, and .../src/test/groovy)
make sure "**/*.groovy" is set as "Inclusion patterns", not "**/*.java"
Hope this saves time for someone.
Cheers!
I had the same error message when I head the test-class duplicated both in the main Java source folder and the testsrc folder. Removing the incorrectly placed one in the main Java source folder solved the problem for me.
2019 Update: This drove me crazy for days even with latest Eclipse and fresh installs (Mac, Grails 4, Gradle 5.1.1, Java 8). Some above examples led me to the solution.
My problem was more that the code I was testing included a mix of groovy and java src/main code. It gave me NoClassDefFound on the .groovy classes when I ran my Spec as JUnit.
Solution: I had to modify my Run/Debug Configuration to include build/classes/groovy/main. Then it worked. It's a little bit of a pain to remember to that for every new Configuration, but, it keeps me going. I hope it helps you.
Whenever you create a Junit test in eclipse, make sure your Junit test file is inside src/test/java folder.
I had a similar problem. Like others have already pointed out, it was about source folders. I had to change my source folder setup. There was an empty src-folder that disappeared after I right-clicked on it and selected 'remove from build path' from Build path menu. After that I right-clicked both java/src and java/test folders and chose Build path > Use as a source folder. And suddenly my tests were JUnited!
In similar situations I'd advice to remove all source folders from build path and add them again when you're sure you've got the right ones. Your source folders should be those with Java package structure under them. In case of proj/java/test/com/stackoverflow/main it's the 'test' folder.
This is what resolved for me (Eclipse Oxygen). I had already done what Robert suggested in the earlier post. I was still getting the error. When I went to edit the configuration for junit launch, I saw that the Test Class field just had the class name. I had to hit the Search button to the right. The Test Class field now had the complete name for the class
com.mycompany.mypackage.MyClass
With this I am able to run the JUnit. But I have to keep fixing this for every run.
Found another way to cause this message. The cause turned out to be an empty copy of MyTest.java under src/main/java, as well as the real one under src/test/java.
Think the empty file was a hangover from some refactoring and was oddly causing no compile errors either. Deleting it enabled the test to run again.

how to exclude external jar while creating executable jar in eclipse or commandline?

I have written a program in Eclipse IDE which uses BouncyProvider class of BouncyCastle.jar. So to compile my class I added BouncyCastle.jar in my project classpath and it compiles perfectly.
Now I want to export my project as Runnable JAR so when I do that from Eclipse, it by default adds the classes of BouncyCastle.jar also in that runnable jar.
But I want to keep my application jar and BouncyCastle.jar different from each other.
How can I achieve this? Can anybody please help?
It sounds like you want to use the "Export JAR File" wizard instead of the "Export Runnable JAR File" wizard. When exporting a runnable jar file, Eclipse attempts to pack everything needed to run the application into a single archive. On the other hand, the "Export JAR File" wizard gives you more control over what is packaged in the archive. You can still create a runnable jar file, but you must make sure to include BouncyCastle.jar on the classpath when you execute the jar. Here are step-by-step instructions:
Click "File | Export". The Export
dialog pops up.
Expand the "Java" folder and select
"JAR file" (not "Runnable JAR
file"). Click Next. On the JAR file
specification page, choose the
classes you want included in the jar
file, and specify the name of the
JAR file to create. Click Next.
On the JAR Packaging Options page,
select options appropriate for you.
The defaults are probably fine.
Click Next.
On the JAR Manifest Specification,
make sure to select the "Main class" for your jar file. This is
the class that will be executed when
you execute the jar file. If you
leave this blank, the jar file will
not be runnable. Click Finish to
create the jar file.
You should be able to execute the jar file by executing "java -jar myjarfile.jar -classpath BouncyCastle.jar" from a command line.
Unfortunately, looks like you can't actually do that. A JAR can't use another JAR that's stored inside itself.
I'd say, unless you have a really strong reason why you can't unpack your BouncyCastle.jar
(like maybe licensing problems?)
just let it unpack (which you can do by adding BouncyCastle.jar as an external archive in Eclipse:)
Right-click on your project
Build Path...
Add External Archives...
Add your archive
Export as runnable JAR)
and watch your package names for conflicts.
Here's an open Java bug ID I found describing your situation
One-JAR may help - a open source solution to your situation
It looks like this has been added in Eclipse 3.5 Milestone 5. See the News for the latest build and bug 219530