Create a jar file from the java code - java

i want to create a jar file from java program i looked at some examples Java code to create a JAR file but it didnt impressed me as this will not create the proper package structure my original command was
jar -cfv formBuilder.jar .\com\accenture\* .\com\wysiwyg\util\XmlUtil.class .\com\wysiwyg\exception\ApplicationException.class .\com\wysiwyg\constants\*.class .\com\wysiwyg\util\FormBuilderUtill.class .\com\wysiwyg\util\SaveFormOnLocalUtil.class .\com\wysiwyg\logger\LogInfo.class .\com\wysiwyg\factory\Validation.class
now i want to do the same using java code but without ant, and proper package structures should be created, is this feasible?

Use java.util.jar package for this work.

Related

Export custom library to importable jar file java

I have created a custom package in Java. The directory structure is like this:-
wolpha
/Stream>Stream.java
/Word/Stream.java
/Pattern/Stream.java
/Stream/Stream.java
So I just made a non runnable jar file and tried to import the package but it gave a error that the package wolpha does not exist. Tried with a main class with imports included but gave the same error.
How can I add all these classes into a single .jar file such that all classes can be imported into any file. I expect some step-by-step instructions with some down-to-earth language.
If you are using eclipse, go to File > Export and follow the instructions as per the screenshots given below:

How to config where Syntastic is going to generate the .class files?

I am writing some code to my phd project and I am using VIM as my code editor.
As I am coding in Java, I chose Syntastic to check and compile my code. So far so good.
My issue comes when I try to create a directory with all my .classes. I want to do this, because then I intend to create a .jar using this directory using a simple make file. So, this is my scenario:
source code:
C:\Users\LABIMD05\workspace\backhoe-nomvn2\src (all .java)
class files:
C:\Users\LABIMD05\workspace\backhoe-nomvn2\bin\classes (where I want to put all the .classes)
In this way, let's say I am coding br.ufrn.Project. When I use :SyntasticCheck, I want the br.ufrn.Project .class file to be generate at:
C:\Users\LABIMD05\workspace\backhoe-nomvn2\bin\classes\br\ufrn\Project.class
and not at:
C:\Users\LABIMD05\workspace\backhoe-nomvn2\src\br\ufrn\Project.class (the same of the .java)
Here goes the options that I am using at my _vimrc file
let g:syntastic_java_javac_classpath = 'C:\Users\LABIMD05\workspace\szz_lib\*;C:\Users\LABIMD05\workspace\backhoe-nomvn2\bin\classes'
let g:syntastic_java_javac_delete_output = 0
let g:syntastic_always_populate_loc_list = 1
let g:syntastic_mode_map = { 'mode': 'passive',
\ 'passive_filetypes': ['java']}
THE PROBLEM:
Everytime I compile br.ufrn.Project file, the .class file goes to the same directory of the .java file
I thought it would be because Syntastic would create the .class file in the current working directory. Then I used:
cd C:\Users\LABIMD05\workspace\backhoe-nomvn2\bin\classes
To see if Syntastic would create the .class in the desired place. But I had no success.
Would you guys have some clue where can I configure it? I just want to separate the .class files from .java files and then use a make file to create a jar with the binaries only. Simple thing.
Thank you for any help you can provide.
You can't configure Syntastic to compile the java files to a different location. However, you can make a command that uses SyntasticCheck and compiles the java files to a different directory.
Using the javac -d dir File.java command you can tell the java compiler where to generate .class files.
Using this you can make a vim command, I called it Javac but you can choose what to call it. It will call Syntastic check and generate the .class files to the other file.
function! Javac()
execute "w"
execute "SyntasticCheck"
execute "!javac -d C:\Users\LABIMD05\workspace\backhoe-nomvn2\bin\classes %"
endfunction
command! Javac :call Javac()
If the SyntasticCheck part is not necessary you can remove that.
Just put this in your .vimrc and then you can use :Javac to execute it.
Alternatively you could also put it in ~/.vim/ftplugin/java.vim if you want it to only be active when editing the java filetype.

Add a new class to an existing JAR File(which contains source code)

I'll try to illustrate the problem as simple as I can.
I have a JAR file, which I extracted using Winrar. (The jar file contains an open source android library).
I want to modify this JAR file by adding a new class to the library.
So here are my steps:
First, I created a class using Eclipse and set the package name same as the android's library package name.
Second, I copied this java File to the folder of the other java files in the library.
Third, I tried to compile the JAVA file via the CMD using javac.
The path of the new java file and the other .JAVA and .CLASS files of the library is: C:\com\example\core\
The name of the new java file would be: "MyNewClass.java"
The command I run via the CMD is: javac C:\com\example\core\MyNewClass.java
But, during the compilation I get many errors saying: Cannot find symbols.
I've been looking up for a solution of this problem but couldn't figure how to solve it and make the new JAR File having another class that I created seperately.
What am I missing?
As per earlier comments:
Rather than trying to modify the JAR, you can get access to the full source code of the Universal Image Loader library by cloning the repository using git or hitting "Download ZIP" on the righthand side of the page you linked.
Once you have the source, import the library in your IDE. From there on you'll be able to build the whole thing from scratch, make any adjustments/modifications you like, etc.
Your classpath might be wrong or there might be some mistake in package name.
When a Java program is being compiled the compiler it creates a list of all the identifiers in use. If it can't find what an identifier refers to it cannot complete the compilation. This is what the cannot find symbol error message is saying, it doesn't have enough information to piece together what the Java code wants to execute.
Try:
javac -cp com/* C:\com\example\core\MyNewClass.java
That should make the compiler aware of all the other classes under com/...

Classpath when re-implementing Java package?

I'm working on creating my own implementation of one of the system Java packages but am having some problems with the wrong class getting picked up when trying to use the package.
For example, lets say my package is: a.b.c.DoStuff and there is an existing Java package with the exact same name, a.b.c.DoStuff.
Using the following code in a test application, I can tell that the system class is still getting used (located in /usr/lib/jvm/java-6-openjdk/jre/lib/rt.jar) instead of my own:
ClassLoader loader = test.class.getClassLoader();
System.out.println(loader.getResource("a.b.c.DoStuff.class");
My package has been compiled into a jar file (package.jar), and I have:
Imported the class in my test file (import a.b.c.DoStuff;)
Added package.jar to my classpath (with both "export CLASSPATH..." and using "java -classpath...")
There must be something I'm overlooking? Any thoughts on how to get my package picked up instead of the system package?
Thanks,
Chris
You can't replace classes from the standard packages unless you put your jar in the special 'endorsed' directory. And some you can't replace at all that way.
Thanks, bmargulies for the tip on the bootclasspath! Prepending my .jar file to the bootclasspath solved my problem. To summarize, I was able to use my own Java package implementation by prepending my package's .jar to the bootclasspath:
java -Xbootclasspath/p:<path-to-jar>

Public Java Class or Jar

I'm looking to create a universal logging file for all of my companies Java applications. I've done something similiar in C++, where you simply import the file.
My question is, what is the most effective/efficent way to create a public Java file (basically what do you create it as, and then how do you reference it)? I'm assuming its by importing your own class or jar? Thanks!
It's not really clear what you mean, but it sounds like you're trying to create a library to be used by multiple applications.
I'd suggest that the other projects should simply refer to it as a jar file - whether they build that jar file from source or fetch one from some company-wide repository which is updated when the logging code changes is up to you. From the application code, you'd just import it as any other class - whether the class is found in a jar file or as a .java file is irrelevant in the consuming Java source code.
It's worth noting that there are already many other logging APIs for Java - I would think very carefully before you create a new one.
In Java you don't import the file you import the class. The class that is imported is either in a compiled .class file or packaged in a .jar file. By convention, package your logger utilities in a .jar and distribute that.
You import the class by name regardless if it's yet to be compiled from a source file or already packaged in a .jar.

Categories

Resources