eclipse doesn't see class file - java

I have eclipse project.
I add directory src/directory and put there SomeClass.java
I compile my application.
in somedirectory in filesystem I see .class file.
But in eclipse somedirectory looks empty.
What the reason? How to fix it?
I press F5 many times!

eclipse hides class files when you are in the package explorer. Try to switch to the navigator (Ctrl+3 and type "navigator") then a "bin" folder should appear and that one contains your class file.

First, the Eclipse src folder and output folder that will contain the compiled classes are two different folders. These two folders' location are configured in the Project's properties page, under the Java Build Path tab. More information on the configuration can be found here.
So when you press in F5 in Eclipse to refresh the folder, you are basically refreshing the source folder, and you will never see the .class file in there. The output folder that contains the .class files are hidden by the Project Explorer view.
If you absolutely want to see these files via Eclipse, switch to the Resource perspective and follow the instructions as given by the answer of this SO entry or rather follow instructions given by #markusw , CTRL-3 + Navigator view is way quicker!

The hierarchy of the source directory src, should be the same as the package name of the classes inside it. In other words, if you have a class whose package name is com.example.util, then that class should be located inside the directory src/com/example/util.

Related

The package prefix is missing in directory tree -- how to handle it with Eclipse?

I downloaded some Java program sources, and I try to import them to Eclipse.
Let's say I have package with name "com.bar.foo.my", and the directory is set like this:
src/bar/foo/bar/my
so sources have their distinct directory but level "com" is missing.
Question: How can I set Eclipse so it would understand this "shortcut"?
Updates:
At the top level the program consists of such directories:
assets
bin
gen
libs
out
res
src
I am showing them to indicate it is not just bunch of *.java files put in the src.
Easy, create a Java project, have the "src" directory be the source folder in your Java build path (this will probably be the default), if not look/change it using Build Path -> Configure Build Path on the project menu.
Then import them into your workspace somewhere (using Import -> Files into workspace) and then create a "com" package under your src folder.
Finally, move the "bar" folder to the "com" package (just drag/drop in the Package/Project explorer).
(You can't have the "com" folder missing as that violates the Java assumption that the classloaders depend on that the file hierarchy much match the package name exactly.)
Easy way out would be right-click on your src folder in Eclipse > New > Package and create a package called com. You can then drag all the other packages inside this to fix your problem.
The reason that I asked you about the package names in your src folder (my comment in your question), was to ascertain how your classes expects the package structure to be. Since they start with com.bar.foo., then in order for them to be compiled by Eclipse.

In a big Java project can not locate .java file belonging to a package using eclipse

I'm new to java with a C++ background. I'm using Eclipse and have imported projects created by maven using "mvn eclipse:eclipse". In eclipse, when I highlight a class name from another class and do "Open Declaration", eclipse would normally open the corresponding .java file.
However, for one of the classes, eclipse "Open Declaration" opens up a .class file which shows all the context of the file. However, this file is not editable. Furthermore, I can't locate the actual .java file behind it. There is a package line in the beginning of the .class file and it points to a directory that doesn't seem exist. I believe there is a way to trace back to the original .java file from the .class file but I'm not sure how to do this. Please help.
The class is likely in one of your dependency jars, so you don't have the source locally in a project. Many maven published projects allow for the source to be downloaded in a jar, and eclipse can use that to display the source, but editing it would be meaningless in that instance.
If you hit ctrl+shift+T and search for the class you should be able to see the location of the class in the bottom portion of the "open type" dialog.
In the eclipse "properties" for the jar that contains the class, you should be able to set the "Java Source Attachment" to the folder on your system that contains the source (if you have it)

Java -- how do I access (import) the class files in a folder that is in the same directory as the src folder?

In eclipse or netbeans or emacs pico or microsoft word or notepad or or or whatever. Thanks.
In eclipse, I'm trying to share a package in one project w/ another project. I do the whole buildpath->link source -> add source ... and it creates a linked directory but not as package within the src/ dir, instead as a separate dir sibling to the src/. so I'm left stranded, wondering, waiting for a solution.
If you mean classes without a package, you can only refer from them outward, you can't refer from a class that is in a package to a class with no package declaration (A very good thing).
Even if you wanted to reference a class in the default package it would be in your "src" folder, not in the same directory as your src folder, so perhaps you mean something different?
Generally your "src" directory would contain a "com" directory--the top of the package tree. So if you had a class in the default package you would place it at the same level as "com", but as I said you can't refer to it from other code--you can only execute the unpackaged class from the command line.
If you are saying that you want all your classes in the same directory or something--then I recommend packaging them all together as a .jar file.
Normally you would add the project (with classes you want to share) to the Build Path (right-click>Build Path>Configure Build Path) of the consuming project. That allows you to compile and run within eclipse.
If you want to create a link from one project to the place where the other project code lives, you have to create your linked folder in such a way that 1) it's already in the /src/ folder or 2) can be turned into a source folder and can see the full package structure necessary to compile your classes. But that is not how most java projects consume the output of another java project.

Creating a separate Folder in the same package... [ECLIPSE]

Is it possible in Eclipse to create a separate Folder in a package? When select "new --> folder", my eclipse version always creates a package. But i want the new folder to be in the old package.
How can this be achieved?
Thanks in advance...
Patrick
What you might want to do, if you aren't putting code in that folder, is remove the folder from the build path. Right-click on the offending folder in eclipse and choose 'exclude from build path.'
Or, you may want a different effect:
Right click on your project and choose add new source folder.
Then in that folder, add the correct package folder structure.
You can have files in the same package located in different folders.
You need to exclude the folder from the source path
In a source folder, all 'new' folders will be considered a new package. It's part of the Java spec that makes all folders from a root to be part of the package structure.
As others have said, this is standard java behaviour and shouldn't really be worked around.
You are probably better off creating another folder outside of the source folder.
However there is a way to do it in Eclipse, be warned that this might come back to haunt you if you later try to build outside Eclipse as other tools won't honour these settings.
You can exclude a folder from the source path by opening the project properties.
Selecting Java Build Path->Source
Select the appropriate source folder and select the Excluded: child item
Hit the Edit... button.
Select the Add button next to the Exclusion patterns: pane
Add your folder and Finish/OK back to the editor.
The matching resources will now be excluded from compilation within Eclipse. It will still appear as a package in the editor though.
You want to create a new child package (bar) of the existing package. (com.foo)
Select new package and name it "com.foo.bar". Eclipse will correctly place it under com.foo.
FYI: Folders under any "source" folder are shown as packages. There is no physical difference except that they appear on the build path. As shown elsewhere you can exclude them but it's easier to just not put them under /src/... to begin with.

How do I include .class files in my project in Eclipse? (Java)

Hey all. I am working on a project for school where we are given the .class file but not the source to include in our code. I am using Eclipse, and I want to include the file in my project so I can instantiate objects from it and use it. The file is TokenizerImpl.class, and I want to use it like this: TokenizerImpl tokenizer = new TokenizerImpl(foo);
I put the file in my project folder, and Eclipse says that "TokenizeImpl cannot be resolved as a type", which I assume means it cannot find the class or source. I tried putting it in the "bin" folder of the project and got the same error. Google search and SO search didn't seem to answer this, so I will give it a shot. How do I do this, oh wise ones?
Edit: Oh dear, I found the problem was something else entirely. These solutions worked fine, but I just forgot to create the Tokenizer interface that TokenizerImpl implements. Doh. Thanks for all your help though, I did learn a lot about eclipse.
You can add a directory containing the class files to the Eclipse project, only if it is inside one of your Eclipse projects, either in a generated directory or in one you have created.
This can be done by adding the class folder to the Java build path of the application. You can set this in the Project properties, by visiting Java Build Path -> Libraries -> Add Class Folder. Keep in mind, that you will have to specify the root folder containing the class files in their packages.
Therefore, if you wish to have the compiler access com.stackoverflow.Example.class present in the classes directory under project A (but not in the build path of project A), then you should add 'classes' as a class folder, and not classes/com/stackoverflow as a class folder.
Project -> Properties -> Java Build Path -> Libraries -> Add External Class Folder
The folder must contain a package hierarchy, i.e. if your class is really foo.bar.TokenizerImpl it must be in the subdirectory foo/bar.
You could also JAR the class files that you want to add and add the JAR file to the build dependencies. To me this is the cleanest solution. Internally the JAR file has to have the correct directory structure, of course.
Right click into your project and select Java Build Path to add new dependencies.
Copying it into the bin folder won't work very well because it is meant to hold the result of compiled sources. As soon as you clean anything the file will be gone.
or put everything into a jar file and add this as an external jar.
Other people have now given better answers. This "answer" was mainly to get information from the OP because the original question didn't really tell us, fully, what had been tried. There are now two answers that truly answer the question in a long-term way.
My original answer is left below for context.
Did you copy it to the bin folder within Eclipse or outside Eclipse? If you did this outside Eclipse then you have to right click on the "bin" folder and select "refresh" for Eclipse to see the new file.
A *.class file in the appropriate folder (depending on its package) under the bin directory should do it.
zip the class folder.
Highlight project name, click "Project" in the top toolbar, click "Properties", click "Libraries" tab, click "Add External jars", add the zipped file

Categories

Resources