As a Java/Eclipse noob coming from a Visual Studio .NET background, I'm trying to understand how to set up my run/debug environment in the Eclipse IDE so I can start stepping through code.
I have built my application with separate src and bin hierarchies for each library project, and each project has its own jar. For example, in a Windows environment, I have something like:
c:\myapp\myapp_main\src\com\mycorp\myapp\main
...and a parallel "bin" tree like this:
c:\myapp\myapp_main\bin\com\mycorp\myapp\main
Other supporting projects are, similarly:
c:\myapp\myapp_util\src\com\mycorp\myapp\util
(and a parallel "bin" tree), etc.
So, I end up with, e.g., myapp_util.jar in the ...\myapp_util\bin... path and then add that as an external archive to my myapp_main project.
I also use utilities like gluegen-rt.jar, which I add ad external dependencies to the projects requiring them.
I have been able to run outside of the Eclipse environment, by copying all my project jars, gluegen-rt DLL, etc., into a "lib" subfolder of some directory and executing something like:
java -Djava.library.path=lib
-DfullScreen=false -cp lib/gluegen-rt.jar;lib/myapp_main.jar;lib/myapp_util.jar;
com.mycorp.myapp.Main
In the IDE, however, When I first pressed F11 to debug I got a message about something like /com/sun/../glugen... not being found by the class loader.
So, to debug, or even just run, in Ecplipse, I tried setting up my VM arguments in the Galileo Debug -> (Run/Debug) Configurations to be the command line above, beginning at "-Djava.libary.path...". I've put a lib subdirectory - just like the above with all jars and the native gluegen DLL - in various places, such as beneath the folder that my main jar is built in and as a subfolder of my Ecplipse starting workspace folder, but now the debugger can't find the main class:
java.lang.NoClassDefFoundError:
com.mycorp.myapp.Main
Although the Classpath says that it is using the "default classpath", whatever that is. Bottom line, how do I assemble the constituent files of a multi-project application so that I can run or debug in Eclipse?
if you want project A to have project B (and its libs) on its classpath, then goto project A's properties, select Java Build Path, then select the Projects tab. Here you Add project B.
Although you may be generating jars for each project, eclipse doesn't pay much attention to the jars. If you are going to have lots of java projects with lots of 3rd party dependencies, you may want to look at something like m2eclipse.
The recommended way of doing things is to configure the build path. This is a nice article describing the process.
Related
I have a utilities file named utilities.java that is located in my ...\NetbeansProjects\Utilities folder and I want to use the methods in it in any java project that I work on.
If I right-click Libraries folder, there is an option to include JAR/folder... so I select that and find and select the Utilities folder and Absolute Path to it as shown, which adds the Utilities folder to the Libraries folder, as shown.
But how do I reference the methods that are in Utilities.java?
import doesn't offer any help and all I get on every attempted reference is Cannot find symbol.
I can't turn Utilities.java into a .jar file (can I??) because the option to Compile is grayed out.
What do I prefix to each such unresolved external reference to successfully link such methods to my code?
Whether I try to just import or import static, I get error, with suggestions to change to some inappropriate import.
What's irritating is that I think I've successfully done this before....
Based on your screenshot, it looks like you are using an older version of NetBeans (perhaps v8.2 or similar). I will assume this - but it would be worth upgrading if possible.
Generally, I would recommend creating two NetBeans projects, and then include the utilities project as a dependency in the application project.
You can use the "Library" features of NetBeans (as shown in your screenshot) but I think it is cleaner to just create projects, which can then be managed by Git/Mercurial/etc. as needed, as separate projects, with no need to rely on NetBeans-specific library management features.
Ant-Based Approach
For an Ant-based approach (the default for NetBeans 8), that would be something like this:
The above was created as follows:
Create the utilities project:
File > New Project > Java > Java Class Library
Project Name: MyUtilities
Create your package and your Utilities.java file. Add the code you need.
Clean and build the project.
Create a project to use the utility JAR:
File > New Project > Java > Java Application
Project Name: MyApplication
Right-click on MyApplication in the project explorer and select the Properties pop-up window.
Libraries > Add Project - find the MyUtilities project and select it, click on "Add Project JAR Files".
Now you will see that project's jar file listed as one of MyApplication's library dependencies.
After cleaning and building the project you will see a file structure as follows:
Note: For NetBeans 12, it's similar, except they now make the choice of an Ant project (vs. Maven, Gradle) explicit, rather than the default.
Maven-Based Approach
It's a similar approach: Two separate projects. In this case they are both "Maven > Java Application" projects - there is no distinction, in contrast to the Ant approach.
The main difference is how you include the utilities JAR in your applications. that depends on how you want to manage your Maven artifacts - and is a bit outside the scope of this question. One way is to follow the guidelines here - but I think there are other approaches, also.
I imported a huge repository from gitlab for my lab project. It is structured like this:
project_folder [master]
JRE System Library
main_project_stuff
anthome_build_stuff
com
config
doc
...etc
Most java projects I have handled are structured like this:
project_folder
src
bin
Because there is no source folder, java will not recognize any of these folders for what they are. As a result, it can't build them and give me error messages. This only works when I drag them into a src directory that I make with eclipse, which is not ok with the lab because I can't be reorganizing files on the master branch when I eventually commit to it with git.
So how can I make eclipse recognize these as src .java files that are in packages like package: com.robot.robotComponent.java? Without actually moving them into the src directory manually?
TL;DR How do I make eclipse stop thinking of these as folders and realize they are actually packages that are stored as a folder tree?
Note: This project is not configured to be used only in eclipse, but is a general project. So I have to make my own .project and .classpath files to make it readable to eclipse.
(I am using Ubuntu 14.04 but maybe that is not relevant. I got it to work by brute force making the project_folder the actual src folder, but then there are a bunch of compile errors because the project really isn't meant to necessarily be built in eclipse? It builds from the command line using run-registry command but I want it to at least show me useful errors like when I make newbie mistakes like one too many "}" or forget to implement all functions in an interface, eclipse can fix that automatically for me with a red error message. Now however I am getting many strange errors, like it can't find project_folder.SuperComponent.java which I know is there!)
Update: I guess this needs to run a registry from ant? And then build it from an ant build.xml. I'm not sure how to get the command line arguments to work with eclipse.
If you use the Eclipse Smart Import feature, the project will be detected as a Java project (even without the .project file) and automatically configured (.classpath, etc.). Instead of using File > Import... do the following:
Clone the Git repository
In the Git Repositories view right-click the Working Tree node and choose Import Projects...
Note, the more up-to-date your Eclipse IDE is, the better the automatic detection and configuration will work.
Alright, so I have a web service that was created using an eclipse dynamic web project. It is currently shared on a CVS repository, but the versioning system used is irrelevant. At the moment, I have literally NEVER been able to pull this project out as is and get it working. It leads to countless errors that cannot be fixed. Every time I need to work on this webservice in a new machine I have to create an entirely new dynamic project, copy over the source files, add all the necessary libraries and make the deployment assembly work correctly again. After finally making it run I share the project as the same one, stop after a second, and then synchronize again (in a way tricking eclipse into thinking this was the shared project all along).
I feel like others must have run into this problem and found a way around it. So if you have a web service or any dynamic web project, what files do you share, and how do you successfully pull it from the repository and get it to run on another machine besides what I currently do now?
Your help is much appreciated,
-Asaf
Edit: After reading some of the responses I feel that this question is actually more specific to those who use WTP to create/test their web services. Just wanted to add the clarification.
Edit2: Let me also clarify that the other 20 or so projects not using WTP are shared just fine. I am able to pull and run them with no problem. Only web service projects are an issue.
In general, you want to check in everything that's not "derived" (generated or compiled - that's usually the contents of the bin directory or other place where your code is compiled/built into). For Eclipse Java projects, you want to include the .project, .classpath, .settings, and any other similar files that Web Tools might create for Dynamic Web projects. The Eclipse CVS client will ignore files marked as Derived so you shouldn't have to worry to much about it.
Without more detail about what kind of problems you've run into, it's not possible to guess what was causing them. My only guess is that perhaps you had different versions of Eclipse and/or the WTP (Web Tools Platform) plugins installed on the different machine. That's just a wild guess, but could explain some incompatibility when you check out the project from CVS.
Bottom line, checking in those .* files is the long recommended approach from Eclipse gurus. Maven can kind of change things, but you didn't mention it so I'm assuming you aren't using it.
I am primarily sharing my experience, may be you can find some help.
Conceptually speaking, the files which the IDE can generate itself while creating new project should not be pushed. I.e the IDE specific files should not be pushed. And everything which the IDE cannot generate on its own must be pushed.
Forexample in case of eclipse, following files should not be pushed:
.settings
build
.classpath
.project
For setting the project on new machine, first pull the files from server, and then create a project from IDE using pulled files.
EDIT: If your project has external jars/libraries, then you will have to add to the classpath manually. You could also push .classpath but that might give errors while creating a new project.
I think it's easiest to use a build system and let the IDE generate the project from your build system.
Eclipse, Netbeans, and Intellij are all pretty good at building projects from maven or ant build files. With this solution you have a simple build that is easy to setup in CI (Hudson, Bamboo, whatever) and you don't have any IDE specific files checked in. If my workspace is totally different than yours, with different versions, plugins, whatever, I'm not stuck with your project file and you're not stuck with mine. My IDE creates the project appropriate for my environment and your IDE does the same for yours.
Since you mentioned having to manually add libraries, I assume you are not using any build manager (like, maven or ant) besides ecplise.
For ecplise to handle the project properly you need the source files (*.java) in their respective directories, any resources bundled with the web service (e.g. services.xml), the ".project", ".classpath", ".settings", etc. files for eclipse. This should be enough for eclipse to generate anything else necessary to build the project.
Any files/directories that are generated by eclipse during the build process (e.g. target & bin directory, *.class, *.war) should not be checked in -- they will be generated when needed during the build.
I am thinking that, since you are adding the necessary 3rd-party jars manually, these libraries might reside in a different path between computers (e.g. if the path contains the username, it will not be transferable to another computer for a different user). To fix that you can set up the classpath using an eclipse classpath variable. In Preferences->Java->Build Path->Classpath Variables set up a varable linked to the "root" folder where the 3rd party jars a stored. Then add the libraries to the project using this new variable, not their full path. To make it work on someone else's computer, you would only need to set this classpath variable to have the build path point to the correct libraries.
It might be beneficial if you migrated your project from eclipse only to a build manager (e.g. maven) that takes care of many of these issues for you. Eclipse can build a project from the configuration of the build manager, making it easier to manage the project.
I wanted to learn Java, as recommended by most of the people I downloaded "Thinking in Java" eBook and Source Code Example Lesson files from mindviewinc.com it's download location is this http://www.mindviewinc.com/TIJ4/CodeInstructions.html
These files don't work with Eclipse IDE for some reasons. So we have to build the files using the Build.xml file included in it's source code (Honestly, I do not have any idea what does "build" means here)
I tried building myself but of no use.. It did started building but after reaching some folder it stopped and gave error to install jboss-osgi-installer-1.0.0.jar .. After downloading it, I had no idea what to do with it, so I double clicked it and it asked me the location to install and I installed it in the default location, which was C:\Users\Username/jboss-osgi-1.0.0 and when I tried C:\Program Files\Java** the installation gave error that "this directory cannot be written, please choose another directory!" but anyway I installed it in the default location and it installed..
and again when I tried to build it, it gave me the same error and asked to Install Jboss....... I'm stuck.. This is happening again and again, wasted many days and didn't even crossed 100 pages of it's pdf because of this reason. It's a good eBook though.
I'm stuck in this from months... Please some one build it and make it like a project which could be easily and send me the project file, it would be really appreciated as I can not the solution to this anywhere on search engines..
Ok here's what I did to succesfully import the sources found in the "accesss" folder of you code samples:
Download the javassist jar as per Bruce's instructions, from here:
http://repo2.maven.org/maven2/javassist/javassist/3.9.0.GA/javassist-3.9.0.GA.jar
You must create an Eclipse project for the sources that are used as dependencies for most of the examples, that's the sources in the "net" folder:
in eclipse make new java project.
select the "net" directory, copy it, then in eclipse select the "src" entry in the project you just made, right click on it and select paste.
after eclipse finishes and auto-builds, it will complain about the missing javaassist jar. Right click on the project ->properties->java build path (on the left)->libraries (on the top)-> add external jars... (on the right), browse and select the javassist jar from where you down load it.
now the "net" project should be ok in eclipse
Now let's create the project for the sources in the "access" folder:
create a new java project in eclipse
Now, because Bruce structured his classes like crap, we'll have to make the proper structure in eclipse for them. This means:
a.create a new package called "access"
go to where your the access folder is on your drive, open it, select ONLY the folders (cookie2, desser, mypackage), copy them, then return to eclipse, right click on the "access" package u just made and click paste
now go back to the access dir on your drive, select ONLY the .java files (all of them, not the dirs), copy them, the back to eclipse, click on the src entry in the access project and click paste.
The access project in elcipse will still complain about missing classes form the "net" resouce. Right click on the project in eclipse->properties->java build path->projects (on the top)-> add(on the right)-> check the net project you created earlier-> click ok twice.
Now it should be ok
I hope you got the idea on how dependencies work in eclipse with this example.
For the other parts of you code samples, create separate java projects, add the classes to the proper package (create the packages if Bruce forgot to), and if you are missing external jars, go to http://search.maven.org search and download the jars. Good luck
"This code is designed to work outside of IDEs. Because packages are not introduced until later chapters, and some of the fancier IDEs like Eclipse require all code to be in packages, if you want to use the code inside those IDEs you will have to make some adjustments" - yep, bruce eckel would say that, cause that's the kind of guy he is. He also said there's ONLY checked exceptions in java, and asked for unchecked exeptions to be added as a bonus.
Now, if your sample code has no package, aka it's in the default package, aka when you look in the .java file, right at the top there's no line saying "package what.ever.bla" then all you have to do is:
-in Eclipse create a new Java se project
- open a windows explorer, go to your .java files, select them and copy them
- then in eclipse, right click the src folder in your project and chose "paste"
The files should be added to a "default" package in your eclipse project, without any error
Here is the simplest steps that I find to create a clean Eclipse project for the TIJ4 source code:
Download the source code zip from: http://www.mindviewinc.com/TIJ4/CodeInstructions.html, and unzip it to a folder, say 'TIJ4-code';
Download the additional libraries as in the previous URL, copy tools.jar from your current JDK's lib directory, and put them into TIJ4-code/lib;
Go into the 'TIJ4-code' folder, and run 'python Eclipse.py' (you need python installed on your system). This script adds the missing "package xxx" line to those source java files which don't yet have them, so as to make Eclipse happy. It also generates the Eclipse project files, which doesn't work well for some reason (beyond my limited knowledge on Eclipse), but they aren't needed here;
Open Eclipse and create a new empty java project, say with name 'TIJ4', choose JRE version 1.5, and "create separate folders for sources and class files";
Copy TIJ4-code/lib directory to your new Eclipse project folder;
Right click on the new project and select "Build Path" >> "Configure Build Path", then go to tab "Libraries", "add jars", then select all the jars under your project's lib folder;
Go to "File >> import", choose "General >> File System", select the unzipped folder 'TIJ4-code' as your "From directory". Check the root 'TIJ4-code' in the left panel of the import window, Click on "filter types" and select the .java, so that unnecessary files of other extensions, like *.class, *.py, *.xml, etc can be ignored during the import;
The zipped folder is no longer needed after the new Eclipse project is built. I hope you find this instruction helpful, and have fun learning TIJ4.
If you want to use this code in IDE rather than Eclipse and so on, for example in Intelij Idea or any other, the best way is to compile necessary classes from the TIJ4 with Ant, but it is not easy for the beginners, although it will be a good practice for you. You should make some changes in build.xml files which exists in every directory of TIJ4. Why?
to change version of Java(now we all use Java version higher than 1.5)
to include javaassist.jar library in the "classpath"
to put all compiled files in 'bin' directory(by default without adjustments TIJ4 build.xml puts .class files in the same directory with .java files. That is not a good deal.
For example you need to import and use within IDE(I use Intelij Idea) the class named net.mindview.util.Print ( see Chapter 3):
download TIJ4 and unzip in any directory you want(for example it would be c:\eckel)
download and install AntApache (you can find detailed instructions how to do it in Google) The result is - type in your console ant -version and you should see the answer. So Ant is working.
download javaassist.jar from http://jboss-javassist.github.io/javassist/ . Unzip to any directory, take out javassist.jar and past it to c:\eckel\net
4.There is build.xml file in c:\eckel\net. Now you should make adjustments. Open it with NotePad++ or other editor.
If you don't have Java 1.5 installed on your PC, so change all 'vesion1.5' to 'vesrion1.8' or other version that you have installed.
after tag description put property name = "classpath" value="./;./javassist.jar"/>
<property name = "src_util" value="./mindview/util"/>
<property name = "src_simple" value="./mindview/simple"/>
<property name = "src_atunit" value="./mindview/atunit"/>
<property name = "destdir" value="bin"/>
Then we make targets that will complile util simple or atunit automatically
Change tag available <available
classname="javassist.bytecode.ClassFile"
classpath="${classpath}"
property="javassist"
/>
Now classpath of available = property named 'classpath'. Classpath is the path where java compiler looks for .class files because some of your .java files want them by having import statement. There won't be error message "You must install the Javassist library " any more
Create targets for compiling util simple and so on See the following link with code
Code with targets
You can modify exists targets not to write all the code.
at the top of build.xml file change default="simple"
open cmd, then cd c:\eckel\net , then ant -simple or just ant (simple will run as default)
Please write if you will have some problems. Possibly i forgot mention about something.
Go to Intellij Idea, press Ctrl+Alt+Shift+S
Press +,
Library or directories
choose your c:\eckel\net\bin
5.now you can use import statement for all classes that exist in net.mindview
6.you can make the same with other dirs.
If you wish to you skip the entire Ant build process, I have converted the project to a Maven project (over JDK8), which you can simply import into your Eclipse workspace.
https://github.com/gauravojha/tij4-maven
Thanks to #Sergey Oliv's example. In addition, I needed to provide uncompiled javaassist package with all java files in the 'net' folder too. The .jar wouldn't work by itself.
I must say, this was a lot of work to get going.
I'm tasked with converting an existing Java/C++ mixed web-application to pure Java, but I'm hampered by some missing Java sources (.java files) for which only the class-files are available. Fortunately I don't need to change anything in that code, just need to continue calling the methods.
I created a new Java Web Application project (using Netbeans); recreated the applet by copying it's sources in and got it working in a skeletal fashion, with the calls to classes & methods not in the sources commented out, but I am now stuck on how to add the class-files (of the missing sources) to this project.
(I'm new to Java, obviously) Any pointers on how I should proceed will be most welcome.
Package the .class files in a jar.
$ jar cvf my-library.jar the/package/*.class
Add this jar to the CLASSPATH of your project/application. In Netbeans:
go to the project view on your left
then right click on the library option,
then click add JAR/Folder option.
To add some source/code to your project, classic java project or webapp java project, you have to declare the path to the needed class/jar in the classpath variable.
If you are running your dynamic web project via eclipse, just add the path to the classpath tab in the "run configurations" of your server.
To learn more about classpath, see wikipedia.
The usual approach is to collect all these class files in a JAR file (use the jar tool) and put them on the classpath.
Different from C/C++, for Java you don't need the source code to these other pieces in order to compile the applet. There is enough information in the class file for the Java compiler to do what needs to be done.
So you can uncomment the calls to this code and follow the instructions in the other posts to put the class files on your project classpath.
In the Project window, right click on your project and select Properties. Go to the Libraries category. Then click Add JAR/Folder and select the location of your .class files.