If I have two projects say projectA and projectB both in different packages, the moment I add projectB to the Java Build Path from eclipse, it is allowing me to import projectB in projectA. Now is there any way I can achieve the same thing from the terminal? As far as I have searched, I did not get a proper answer.
Thanks in advance.
You should build both projects and package them as JAR files. Then on the command line you include both JAR files on your classpath
For example place your JAR files in a sub-directory named lib then you can execute your application like so
java -cp ".;lib/*" my.package.MainClass
In this example I have used the Windows path separator ; inside the quotes used to define the classpath. The dot . denotes the current working directory which you may or may not need. The class my.package.MainClass is expected to exist in either the current working directory, or in any *.jar file in the lib directory.
Related
I created project in netbeans. In Netbeans it compiles. But i need to know the way of compiling the project in Linux. It simply not just a file. I have folder which contains Build,nbproject,src and etc.
Please tell me the way of compiling a project created in netbeans in linux terminal.
In the most basic case you compile all .java files in the src folder. This generates .class files. You then execute the main class file (which contains a main method). Don't forget to also put all other dependent classes on the classpath.
Compile:
$ cd <yourproject>/src
$ javac $(find . -name "*.java")
Run:
java -cp ./ yourpackagename.YourMainClass
the -cp argument specifies the classpath. All the classes on this path will be considered as dependecies. In this case we put the classpath to ./ which is the current directory (src). That means if MainClass uses other classes in your project, they will be linked (made usable). If you do not specify a classpath your whole application would need to be contained in your MainClass, which is basically never the case.
Another way - much more common - you create a jar archive (basically a zip file) which contains your compiled classes. I'm pretty sure you can generate jar files within NetBeans. That generated jar file can then be executed:
$ java -jar myjarfile.jar
Most people do not directly use the IDE in order to generate the jar but rather use build tools such as Gradle or Maven.
Another option is using the jar command:
https://docs.oracle.com/javase/tutorial/deployment/jar/build.html
Java creating .jar file
Hope this helps.
I tried to use a sourceforge library in my program.
On my computer I use Eclipse, and I easily add the jar files to my project.
Now, I want to move the code to another computer.
I tried an executable jar file, but the problem is I cannot debug it on the new computer.
So, I decided to move the source code and compile it there.
I tried the following but defeated in all of them: (all in Windows Command prompt)
Copy the jar files in the /lib/ext folder of my jre folder and add this folder to classpath
javac -cp ".\lib\*.jar" src/*.java
javac -cp "./lib/*.jar" src/*.java
In all of them the classes that are defined in the library jar files can not be recognized by java!
Actually the package doesn't find...
Any idea? Any stupid thing that I am doing?
The correct wildcard for matching all jars in a directory is just
-cp "dir/*"
Please see the Understanding class path wildcards section of this page: Setting the classpath
I used eclipse to create executable jar. It relies external other jars.
In Eclipse, It is simple that you just need to choose Extract required libraries into generated JAR.
You can create an executable jar. It can be executed any places where jre is installed.
But If I use command line to compile jar.
javac -classpath [external jars] *.java
jar cfm [a name].jar manifest *.class [external jars]
It can generate jar. But the jar can only be executed in the directory where it is produced.
If I put it into another directory or machine, it complains NoClassDefFoundError.
So, my question is that how I can generate executable jar using command line as Eclipse.
A jar file cannot have its dependency jars inside. In case of Eclipse, it will unpack all the classes from the dependency jars and will bundle it into your single jar along with your class files. If not in the eclipse way, you need to
1) Create a manifest file which lists all the dependency jars
Manifest-Version: 1.0
Main-Class: Your Main class
Class-Path: dependency1.jar dependency2.jar dependency3.jar
dependency4.jar dependency5.jar
2) Create your jar with your class files using the class path including all the dependency jars and using the above created mainfest file.
3) In this same folder where you created your jar, place all the dependency jars.
Now your folder will look like this,
yourjar.jar (With the manifest file you created above)
dependency1.jar
dependency2.jar
dependency3.jar
dependency4.jar
dependency5.jar
4) Now if you want to share this, you need to share this folder and you can launch your jar from this folder. This is your executable folder and you can run it from anywhere.
Eclipse use Ant to package jar file, you can save the ant script that eclipse use to generate the jar checking the checkbox Save Ant File in the export window :
so, you can generate the Ant Build.xml script and then execute it using ant directly from the command line without using eclipse anymore if you want.
My preferred method for creating an executable jar is to use a utility called one-jar. I have a blog post discussing how to use it in maven and ant: my one jar blog post
Does anyone know how I can export individual packages/class files into a jar with IntelliJ IDEA 10.5.2 instead of exporting the whole project?
Thanks.
Create separate module (right click on the project, then New -> Module) for each packages/classes set you want to be a separate jar.
For anyone who comes here for the answer:
You can make the jar using the command line. All you have to do is, run the command in the parent folder of the package you want to export as jar.
e.g You only want to export "mypackage" which is situated in "project/src/mainpackage/anotherpackage/mypackage"
you go into "anotherpackage" and run the following command.
jar cvf myjar.jar mypackage/
for more information on the jar command, take a look at the documentation: https://docs.oracle.com/javase/tutorial/deployment/jar/build.html
NOTE:
if you make a jar of only the source files i.e. .java files inside the src folder, you won't be able to use it as a library in other projects.
Be sure to make a jar of the .class files(i.e. compiled files) for such use.
You can quickly export the jar package you want to export by installing the Handy Export Jar plug-in.
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 ...).