Compiling Java Program with .jar file and different packages - java

I have this code, in where I have 'jar' file that I use as a library. And in my "src" I have multiple packages with ".java" files in them.
I am highly confused about how to compile and run this program on terminal.
I have attached an image of the directory.
Link: https://www.dropbox.com/s/yfg22v9y78yndan/DirecotryPic.png
Thanks in advance :)

Why do you have a jar file in the src folder??
You should be referencing them from Java build path. Right click on your project -> Java Build Path -> Configure Build Path -> Libraries.
Add your library files here. Not in the src.
From the picture it seems you are using eclipse IDE. Eclipse builds the file automatically. From the Menu bar click Project and make sure you checked Build Automatically.
To run this on the terminal, export this whole project as Runnable Jar and from terminal execute this "java -jar yourproject.jar"
Right click on project -> Export -> Runnable Jar.
Is it what you are looking for???

First of all: JAR files should not be copied to the src folder. As far as I can see DirectoryServer.jar is in it right now.
Secondly: .sh files should be moved to the root of your project too.
You could easily generate Ant buildfiles with Eclipse (Export.. > Ant buildfiles) and then use them from terminal (installing ANT is a must!).
Other option would be to export a runnable JAR directly from Eclipse, then run it from terminal.
I hope it helps! Feel free to ask in comment if you have any more questions!

Related

How do you export multiple java files as a singular runnable jar file in Eclipse?

I have a project with 3 java files:
Title_Screen .java
Game_Screen .java
Game_Code .java
Game_Screen and Game_Code use OpenGL, and Title_Screen opens Game_Code which opens Game_Screen. In eclipse, the program works perfectly, but when I try to export it as a runnable jar file, no matter what I do, it always just exports Title_Screen.java. What am I doing wrong, and what steps do I have to take to export all three java files in one .jar file?
Edit: It seems to only happen to my program, perhaps it's something to do with the OpenGL libraries?
Edit 2: I removed the libraries from my program, same results as exporting it to a jar file. My actual problem is that I can't put in the libraries.
Edit 3: Problem resolved! All I had to do was use jarsplice to create my runnable jar, not Eclipse. Tutorial I used: https://www.youtube.com/watch?v=YqGUk84BmlQ
You can use the following command to build the jar and specify the main class entry point (Main).
jar cfe output.jar Main src/Repository/* src/util/*.class
you can write multiple files when creating the jar
jar cf Output.jar src/util/Main.class src/util/SubMain.class src/Repository/*
Or from eclipse
Put all your files in a folder in your Eclipse project and then:
1.Right click in your folder
2.Export
3.Java -> Runnable JAR File
What steps are you following to export it as runnable jar?
Are you able to run the jar file successfully?
Steps to export Runnable jar is :
Right click on project and select "Export" option.
Provide the destination path where the jar file needs to be store.
Export

Compile and run Eclipse Project from command prompt

How to compile and run Java Eclipse Project from command prompt?
How to run a Java Eclipse project from Command Line with java file name only. I don't want to to use class file or jar files generated by Eclipse.
Is it possible?
Even with jar file, I found loading of static file was failing, as FileNotFoundException, how to solve that?
I meant to run like this-
http://www.skylit.com/javamethods/faqs/javaindos.html
First javac then java
Kind of old question I know, but if you want to know command prompt for running Eclipse-based project (i.e the one that Eclipse uses)
Run your project into Eclipse
Goto Debug perspective
(on my screen anyway) Window in top left corner should have a little
'debug' tab.
Right click on name of your project, select Properties at
the bottom of drop-down
Click on the 'Command Line' field (this is what you
probably want).
Press [ctrl]+A & [ctrl]+C to select and copy
Either paste this into command line, or
(what I did) in Windows, create new *.bat text file and paste it in
there ... now double clicking on that file should run your java proj.
Pretty useful if you wanna run something out of eclipse and are lazy like me.
BTW I needed this for exporting project for a uni assignment. Of course, they wanted to see code in *.java files too and above just uses *.class files Eclipse builds on the fly.
To make batch compile your *.java files & then run you need to put together an appropriate javac command before the javaw line you got from process above and adjust accordingly - look at Java Docs for this. Eclipse has done most of hard work with library class paths though (I was using a few libs).
For building you can export an Ant build file. Just right click on the project -> Export -> Ant buildfiles. On the command promt use ant <buildfile> to build the project.
Take a look at this answer: Eclipse: export running configuration for running the eclipse project from the console.
Assumes a project with the following directory structure:
PROJECT_HOME
-> lib (jar files)
-> src (java code)
-> hirondelle (top-level package; no .java files)
-> ante (.java files)
-> deluvian (.java files)
Compiling With javac
PROJECT_HOME>javac -cp lib\* src\hirondelle\ante\*.java src\hirondelle\ante\deluvian\*.java
This compiles in place, and creates .class files beside .java files. If you want to place generated class files elsewhere, use the -d option to put them into an existing directory:
PROJECT_HOME>javac -cp lib\* -d build src\hirondelle\ante\*.java src\hirondelle\ante\deluvian\*.java
If your jars are in various directories, then the classpath is a list delimited by semi-colons:
-cp lib\*;C:\abc\one.jar;C:\xyz\two.jar
For more information refer below links:
Compiling With javac - javapractices
Build Eclipse Java Project from Command Line - Stackoverflow
Select the project and click on File->Export which opens a new window.
From that select Runnablejar option and click next button.
In launch configuration select your main class and in export destination give the path where you want to store the jar file.

Jar file not finding libraries

So I have a small Java program with some gui that I have runnning in eclipse just fine. I followed the fatjar tutorial to create the .jar, but when I try to run the .jar the first gui window appears but none of the functions work. When I ran it in cp, clicking the button generated an exception basically saying some of the object types could not be resolved as type, all of which were from import libraries. Has anyone seen this before, I tried using this program jarfix for an issue similar to mine, but nothing.
Please define
When I ran it in cp
The format of the java command should look something like
java -cp /path/to/jars com.main.class
You could start locating the source of the problem by opening the jar file with an archive program (e.g. winzip) and look if the class / type is included in the jar file or not.
If it is there it might be a classpath issue. If it is not there something is wrong with building the jar file. Musn't the included jar file be set in the "Order and Export" tab in the java buildpath dialog?
I am not aware of fatjar. But I faced similar problem, might be somewhere it is linked to your issue.
In my case everything was working when I was running through eclipse, but when I created JAR using Eclipse I faced issue as what you are facing, but in my case it was issue of accessing resources inside JAR File.
So solution was,
Right Click -> Export as -> Jar, here check the box "Add Directory Entries" and then create the JAR. Ans all worked.
Above all, first you should check whether the jar which is generate is correct or not by just open it with winrar or similar tool and see all classes and resources are placed properly.
An alternative would to place all those jar files in the same folder. i.e. if your application jar file is App.jar and other dependencies are A.jar and B.jar. Then drop A.jar and B.jar in the same folder as App.jar.
If you double click on App.jar it will by taking the other dependencies in the same folder by default.

Run Java project (built in eclipse) using Batch File

I want to run my java project which i've built using Eclipse IDE. Now my goal is to create a batch file which will execute my project with one click. i referred question , but didn't get any idea. Please give me a solution. Thanks in advance.
Because you are working in eclipse, this makes things easier. First, export the whole program as a executable jar. You can do this by going to Files>Export and then in the pop up go to Java>Runnable Jar. Then follow the steps required to make it. Next you make a .bat file and write the following code.
start javaw -jar NameOfJar.jar
Make sure that you put the file in the same directory as your jar. Now you should be able and click on the .bat and execute the program!
1.Open Notepad & write
#echo off
javac YOUR_JAVA_FILENAME.java
java YOUR_JAVA_FILENAME
2.Save-As executeJavaProgram.bat
Make sure that YOUR_JAVA_FILENAME.java resides with your batch file and java path is set in environment variable.
3 . Double click on batch file.
Follow this tutorial to create a jar file of your eclipse project.
After doing it create a batch file in the same folder where you exported the jar with the command: java -jar yourjar.jar
Create jar file using eclipse i.e right click on your project select export jar file then provide file name to store your jar file. In this file eclipse will keep all .class file only and in META-INF folder main class definition if your are creating executable jar file.

How can I compile a Java program in Eclipse without running it?

I would like to compile my Java program in Eclipse but not to run it. I can't understand how to do it.
How can I compile a Java program to .class files in Eclipse without running it?
You can un-check the build automatically in Project menu and then build by hand by type Ctrl + B, or clicking an icon the appears to the right of the printer icon.
You will need to go to Project->Clean...,then build your project. This will work, even when your source code does not contain any main method to run as an executable program. The .class files will appear in the bin folder of your project, in your workspace.
Right click on Yourproject(in project Explorer)-->Build Project
It will compile all files in your project and updates your build folder, all without running.
In the case that you delete your .class file in Eclipse and then try to build it again from the .java file it will do nothing. If you try to run the .java file without the .class file you will get an error that it can not find the main class.
You will either have to change and re-save the .java file then build it again, or else you have to run Clean on the project then build again.
Try this in your console:
javac {$PathToYourProyect}/*
If you also need any external library, try:
javac -cp {$PathToYourLibrary}.jar {$PathToYourProyect}/*
Right click on the file on package Explorer
Then go to Show in
Under it go to terminal
Eclipse will have a terminal then
Use javac fileName to compile
Go to the project explorer block ...
right click on project name
select "Build Path"-----------> "Configuration Build Path"
then the pop up window will get open.
in this pop up window you will find 4 tabs. 1)source 2) project 3)Library 4)order and export
Click on 1) Source
select the project (under which that file is present which you want to compile)
and then click on ok....
Go to the workspace location of the project open a bin folder and search that class file ...
you will get that java file compiled...
just to cross verify check the changed timing.
hope this will help.
Thanks.

Categories

Resources