I cannot include the jar files in my project - java

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

Related

Compiling a Net beans created project in Linux terminal

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.

Adding a project to java build path from terminal

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.

Decompile, edit and compile a .jar file

I am working on a program that automaticly decompiles and compiles java programs. The decompiling works now. But now i am stuck on the compiling of multiple java files in multiple packages. Can anyone help me to find acmd command for compiling a java project?
And if possible make it support external libraries
I hope i did not make that much grammar mistakes English is not my native
language ;-)
Lets say you are at the application root folder trying to compile App.java file from com.example package that uses lib1.jar and lib2.jar libraries from lib folder to a destination bin folder, compilation command should have the following format:
javac -d bin -sourcepath src -cp lib/lib1.jar;lib/lib2.jar src/com/example/App.java
So 'bin/com/example/App.class' file should be created. If App.java uses other classes from the project, they all should be automatically compiled and put into corresponding folders.

Eclipse Java project: can I set eclipse to store .class and .java files in 1 folder?

I just need a simple environment of .class and .java files in a single folder so that I can execute java files later on using "java xx" in command line without adding any extra syntax. I'm not planning to use packages or sub-directories in my project and I won't be executing any files directly from eclipse either.
You could create a executable jar file and run it from command line using:
java -jar MyApplication.jar
Here is the link to create an executable jar file from eclipse:
How to create an Executable jar file
I think you are looking for this:
Create new project->Java Project->In the Project Layout section change to 'User project folder as root for sources and class files' (using Eclipse Kepler, it should be similar in other versions of Eclipse).
But keep in mind that it might become a mess after some time.

compile a java project with different packages

I have a java project in eclipse with different packages. How can I compile the project from command line in unix (bash) ? Using plain javac doesnt seem to work. For eg, in eclipse I have
src
...server
...client
...shared
And the main file that I want to run later is in the server package called server.java
You will have to add all the source paths and dependency .jar files into the compilation call. The easiest way to do this will be to get Eclipse to export an Ant build.xml for the final jar you are trying to compile. This autogenerated ant file is usually pretty messy but gives you an idea of how the project should be built.
javac -sourcepath /path/to/src -d /path/to/classes

Categories