What is meant by saying project is on the classpath? For example, I keep a file within project directory (Project/file), is that on the classpath? How should I know what the classpath involves in my project?
I am trying to read text file by using InputStream object. The idea is to read the file whilst you are running the jar file (the app). As far as I know in case of InputStream object the file shall be on the classpath. So, where does the file supposed to be? I am developing on Eclipse IDE.
Thanks
The project's Java Build Path determines what your project's sources consider to be on their CLASSPATH, as well as what's used when launching applications in that project. If you're running a .jar externally, it's what you've set in that environmental variable or with the -classpath/-cp arguments.
Classpath during eclipse development is determined by Configure Build Path dialog. Classpath during runtime is determined by runtime conditions. In eclipse it is done by Run (or Debug) configurations dialog. If you deploy your application somewhere, then classpath is determined by conditions at deployment place.
Related
I made a simple standard-lone java Application using Spring,Apache Camel,Activemq for processing messages.
Note: My Application don't have any GUI.
My project structure is in the following way.
SACLib folder have nearly 70 external jars(all Spring,Camel and Activemq corresponding jars).
It's working fine in Eclipse. SO Now We want to deploy into Jar file.I tried in Eclipse,But I didn't seen Rod1,Rod2,Copy1 and SACLib folders in my Jarfile.
after Deploying Jar, If I run FirstConsumer.java it runs Rod1-->ThMapInfratab1-2.exe file. For this I mention Real paths of .exe file.
How can I make Jar file with including all my folders.
Thanks
Well, this is a kind of work that is typically done with build automation tools like Apache Ant, Maven or Gradle, so you can investigate there if you want to make this happen automatically next time.
But, if you want to do it manually...
First, you project needs a META-INF folder where you will place a file called a MANIFEST.
That manifest contains a Main-Class entry pointing to you main class. You can read about this in the Java Tutorial: Setting Application's Entry Point.
But it can also contain a Class-Path entry, pointing to all other jars required by your application and that should be loaded by the executable jar.
You can read about it the Java Tutorial: Adding Classes to your Jar Class Path.
If you are building your executable jar with Eclipse, it will let you choose the MANIFEST file that you want to use during the creation process.
Now, if you want to use build automation tools, there are other answers here that explain how to do it:
Creating a bundle jar with ant
How to create executable jar with dependencies with Maven
How to export an executable jar in Gradle
simply using ant download it , and then make a build.xml file and put it
Here's an simple example of an ant target that will create a jar (named test.jar) that includes all jar files under the lib directory. Maybe this will solve your problem?
for using apache ant, see this
http://ant.apache.org/manual/using.html
Hi, I am developing an application in Java. I have to run that jar in different Operating Systems, such as winxp, linux, rhel, Ubuntu.
I have some jar files that must be in the classpath.
The client is not ready to set the path, so I have to set it programmatically.
My problem is with the different Operating Systems, because the user will have different paths. How can I get the path through a config file?
How do I manage OS and these dependencies?
Any idea if I can set the path at runtime?
You should package all the jars your program depends inside your executable jar. In your project create lib directory and push all the dependencies inside. This will save you lot of pain.
If you use Maven2 , you can do something like this in your pom.xml.
In case you build from IDE (Eclipse or IntelliJ etc) you can use the wizard: R.click your project root -> Export -> Runnable JAR etc .
Background
I'm using an Eclipse 4.2 (Juno) release to build a plugin for a Java application. The source code and classes for my plugin all reside within the project workspace. The application jar and its source code are, for various reasons maintained outside of the eclipse workspace and the application jar is produced by another build mechanism.
This isn't a problem as I have referenced the application jar file in my project using drag and drop and the 'Link to files' option specifying it 'Create link locations relative to: MYDEV', where MYDEV is a Linked Resource Variable I have created for each Eclipse installation. This lets me use the same eclipse project on multiple machines where the path to the application jar varies, but is always the same relative to MYDEV.
However, I cannot find a way to associate the application source code with this jar unless I use an absolute path. I only want access to the application source for debugging purposes.
Question
In Eclipse, how do I attach java source to a referenced jar in a way that allows a project to be used on multiple installations where the referenced source code has been relocated?
If the source is outside the proyect, then there's no way to access it without an absolute path.
The best solution is using a symlink inside the eclipse project directory to the actual source folder; eclipse won't notice the source is outside the project directory and everything will work fine, without having to relocate the source.
This is the only way I was able to do what you are suggesting (do not know if there are better ways).
Package a JAR file that contains the source code of the JAR file. Place it inside your project in a folder (you do not need to add it to your class path)
In the eclipse project right click on the JAR file and go to "properties" and then to "Java Source Attachment" From there select "Workspace" button and pick the jar file with the source code we added from the above step.
So the source attachment path will always be relative to the project. That way if you share the project via SVN, GIT or whatever, the source will always be available and it'll work if the user is running eclipse on Windows or Linux.
I've had a similar problem to you where absolute paths were a problem for people using different OS and not even using mapped drives via Samba helped that much.
Hope this helps.
The way I have solved this problem is to use a use defined library in eclipse. Here the steps that you can use to solver this problem.
Create a simple project in eclipse in the directory that contains the jars and the source code files.
Define a java user library add the jars to the user library and for each jar in the library specify the location of the source. If the source files are in a project that is in the workspace then the path will be relative to the workspace folder.
Export the user library as an .xml file
When another use wants to setup an eclipse workspace with the same setting as yours they will do two things.
Import the project that contains the jars and the sources into their workspace.
Import the user library into their workpsace.
I'm trying to make this http://htmlparser.sourceforge.net/ code run in eclipse.
There instructions are simply "To use the library, you will need to add either the htmllexer.jar or htmlparser.jar to your classpath when compiling and running."
I've added htmllexer.jar and htmlparser.jar to my build path and everything compiles fine. But at runtime eclipse can't seem to find those jar files. I'm not sure how to add those jar files to the runtime classpath....
My eclipse looks like this:
For a web project you have to drop those libraries straight in the /WEB-INF/lib folder of the project. This folder participates in webapp's default runtime classpath. In Eclipse, you don't need to fiddle with the build path properties this way, Eclipse will do all the necessary magic. Don't forget to undo the changes you made before.
If I understand well, you are creating a web application. In that case, you have to configure properly the Deployment assembly section of your project properties.
I've downloaded a new api for Java that accesses excel files, but am unsure on how to install it so that it can be imported for use in my program. Help is appreciated. Thanks
To the point: just put it in the classpath.
A classpath is basically a collection of disk file system paths to a root folder where all classes are located like /path/to/package/root and/or paths to the JAR file itself like /path/to/file.jar. You can specify multiple paths in the classpath by a separator character. In Unix based systems like OS X the separator character is the colon : (on Windows it's the semicolon ;).
How and where to specify the classpath depends on how you're compiling and executing the program.
If you're using plain javac to compile the program, then use the -cp argument to specify the compile time classpath. Or if you're using an IDE, then add it to the project's Build Path (which covers both the compile time and runtime classpath).
If you're using java to execute the program as a simple .class file, then use the -cp argument the same way. If you're using java -jar (or doubleclicking the file in some platform specific UI explorer) to execute the program as an executabele .jar file, then you need to specify it in Class-Path entry of the JAR's MANIFEST.MF file. This one can be relative to the JAR file's location.
You don't really have to "install" it - you just have to put it inside the Classpath. For example, if you're using Eclipse, you can right-click on your project, select something like "build path"->"configure build path", then libraries.
That depends on the tools you are using for development. Basically it will have to be included on your classpath for your IDE project for development, and in your runtime classpath at deployment time.
How to accomplish this in development is specific to your project configuration, IDE and how you store dependent jar files in your development environment (i.e. shared lib directory, maven, project lib folder ...).