How to create an executable jar in Android Studio? - java

I have Java project in Android Studio. I am able to launch and test it from Studio, but I have no idea of how to build it and get an executable jar file. Need help how to do that. I see some jar files in the "build/libs/" directory but thats not what i need.

So first of all Android Studio is the official integrated development environment (IDE) for Android platform development and for Java projects (as well).
Yes you can create and execute java projects hassle free but when it comes to making a Java Archive or an executable JAR then it becomes really unpleasant (there's no easy way to do this). But if you still want to go for it then maybe this will help you.
Anyways being a Java/Android programmer myself I prefer doing tasks on separate IDEs. For Java I use Eclipse-Neon. Exporting a JAR, or an exec-JAR learn how.
If you're adamant about using the Studio then I'd recommend you to start new by choosing java module as the project type.
Hope this helps.
Thanks

Related

How to integrate external packages in java?

I'm trying to develop an app that uses Bluetooth technology, so I need javax.bluetooth & javax.obex & javax.microedition packages. I've downloaded it but I don't know how to properly place it and where so the Java can see it while compilation. How do I integrate it?
It depends on what tool you're using to create your jar. For example: Maven, Eclipse or IntelliJ. Usually you can find an answer that is relevant to your build method by simply googling it.
However, if you want to permenantly add them to your JDK, you'll have to paste them in your JDK's integrated jre into the following folder: \jre\lib\ext
For me that would be:
C:\Program Files\Java\jdk1.8.0_231\jre\lib\ext
As you can see, I added RXTX (a serial comm library) to mine and I can now use it in my code.

How do I use ProGuard for Eclipse Java desktop applications?

I just installed ProGuard for Eclipse (http://marketplace.eclipse.org/content/proguarddt), curious about how to protect my exported .JAR program from decompilation. However, I don't have the faintest idea how to use this.
When I search around, all I see is how to use it with Android. But I am just making a desktop Java application.
They all speak of some sort of configuration file, but I don't know where to put it or how to create it or how to use it.
At How do I use ProGuard? he mentions a .pro file, but in Where do I find proguard.cfg in my project? they talk about a .txt file? I am too confused at this point.
I have an Eclipse project. It is a Java desktop application. It has a number of apache-commons libraries in its build path. It also uses JavaFX (if that makes a difference). There is only one main method in a class called "App".
Can you explain to me how to use ProGuard in Eclipse for such purposes?
Have a look at examples here suitable for your application type: http://proguard.sourceforge.net/manual/examples.html#application
This answer can give you the start:
https://stackoverflow.com/a/2124256/1129313
Here is the usage of proguard: http://proguard.sourceforge.net/manual/usage.html
Another helpful link: http://www.thegeekstuff.com/2008/06/protect-your-java-code-from-reverse-engineering/

When would I use ant for Java web development?

I do not understand the purpose of ant. I read the introduction, but am missing the big picture. I know the following (or at least that I want to use them),
Java
jdbc
hibernate
jsp
servlet container like Tomcat
But I don't see how ant fits in.
Is there some all comprehensive tutorial that tells me how to put all this together?
Why would I want ant? What is the alternative if I don't have ant?
EDIT: So is this sort of the compiling part of its Visual Studio counterpart?
Ant is used as a build system. You do not have to use it--in fact there are quite a few better systems but since ant was one of the first it's still likely to be very common. It generally is used to compile your java files, create wars and often even put the .war files onto the tomcats.
Maven is bigger and tries to do a lot more, but it's also rather strict in it's format and quite heavy--but it's quite common these days as well. Maven can gather all your dependencies automatically which is nice, with ant you still have to download everything yourself.
There are others, but you get the idea.
Response to comment:
I don't use visual studio, but I think it is more limited. For instance, Eclipse can build all your java files into a jar or execute them directly (in fact, it uses ant internally). If you wanted to take your visual stuido "Build configuration" and put it into another tool (Perhaps an automated build system like Jenkins) that doesn't rely on visual studio directly, can you do so?
I guess Ant is the way we code our builds--it's portable betweeen many tools. Visual Studio would be cleaner to "create" your build, but "Exporting" the ability to build would be more difficult (if even possible).
Also ant is portable across operating systems as well as tools. We can switch from Eclipse to Netbeans to IntelliJ without touching our build toolchain.
How would you completely remove visual studio and replace it with slickedit, emacs or vi (if that was your thing, which I'm sure it's not) without starting over from scratch with your build?
For any web application to work you need to package or build all your java code with all the libraries into an web application archive to accomplish this task we need to some tool or library to package into the correct format and some of the libraries which can achieve this is
Apache Ant and Apache Maven
There some good discussions about Ant vs Maven in SO.
You need ant to build the project. Well you can build project using IDE like eclipse etc. But for production purpose and big projects, it always advisable to some build script like ant. Its a script where you can customize what you want to build and what not. you can define what is the output should be like war/jar/ear etc
Other alternative is Maven which is very prevalent and standard now a days

How to make installer pack of Java swing Application Project?

I've created a java project using swing application with JMF. I've tried to convert it into package but it failed.
The jar can be created but it won't work in other computer (Windows). Can someone know the answer? Please tell me.
I need to package the project as installer and it would work in other system. Guide me for this.
This link gives a list of opensource installers for java applications:
http://java-source.net/open-source/installer-generators
I'd recommend izpack, its used by a lot of software like jedit, scala installer, etc.
You can try install4j. (Its not free though, you may try evaluation version first)
Here is list of Open Source Installers Generators in Java :
http://java-source.net/open-source/installer-generators
I have used Install4j and it works
You can use the install4j for the create exe file for jar file of java project and use the Inno Setup Compiler file for create the installer for the project.
this ts Youtube vedio for the how to create the exe and installer
video for create the exe and installer

Java equivalent to VS solution file

I'm a C# guy trying to learn Java. I understand the syntax and the basic architecture of the Java platform, and have no problem doing smaller projects myself, but I'd really like to be able to download some open source projects to learn from the work of others. However, I'm running into a stumbling block that I can't seem to find any information on.
When I download an open source .NET project, I can open the .sln file with visual studio and everything just loads. Sure, there's occasionally a missing reference or something, but there's really very little configuration required to get things going. I'm not sensing the same ease of use with Java. I'm using eclipse at the moment, and it feels like for every project I have to create a brand new Eclipse project using "create from existing source", and almost nothing compiles properly without significant reconfiguration. In the case of web projects, it's even worse, because Eclipse doesn't appear to support creating a web project from existing source. I have to create a standard Java project from source, then then apparently modify the project file to include the bindings for the web toolkit stuff to work properly.
Assuming I want to be able to contribute to a project later on, I shouldn't have to be making such drastic changes to the file structure to get my IDE to a workable state. What am I missing?
The best way to go about this, is to first remove the IDE from the equation. In C# there is only one environment, so the presence of the default IDE is assumed. In Java a default IDE does not exist.
In the end Java is all about java source files and supporting jars. If you figure out what those are, your 99% of the way home. Then you can apply you favorite build system for the set. Some project require a runtime environment, like a webserver to handle the JSP files. If you understand what the basic setup is (as specified by the specification) you can quickly setup your IDE to handle that.
If I get a project with java files and supporting jars, I fire up Eclipse, create a new project, point it to the project's base directory and Eclipse will automatically detect what it finds and set up the project accordingly.
But projects often come with a build environment included. The trick is to figure out which one:
if a build.xml file is present, it is using ANT. This is a "make" like tool. You can execute "ant" in the directory where the build file is (if you have ANT installed) and it will try to compile. All IDE's like Eclipse and NetBeans recognize the build.xml file and allow for starting ant from inside the IDE. There is no guarantee the supporting jars will be present.
if a pom.xml file is present, it is using Maven. Maven is also a make like tool, but enforces a much stricter build cycle. Plus (and this probably is its biggest advantage) it automatically downloads supporting jars. If you have Maven installed you will be amazed at what it downloads... just sit tight, it'll work out in the end. IDE's usually require a plugin to support pom.xml, but then you automatically have the whole project setup at once.
if a .project file is present, it usually is a Eclipse project
if a nbproject directory is present, it is a NetBeans project
Getting to know a build environment / IDE is more work that trying to setup a project in the one you know. So I always try to get it running in Eclipse. Usually projects are quite simple to get running once you know your IDE.
Having multiple ways of doing things is not always pleasant, but it's the cost of having an open community. If there is only one IDE it makes things easier, but I like the fact that there are more people trying to figure out what the best way is to get things done.
In some cases you really may have to make drastic changes. A well-designed build system will require no configuration at all on most platforms and perhaps a few changes on exotic platforms. However, there is no single standard build system for Java; some people use Eclipse, some people use Apache Ant, and others use Apache Maven or Apache Maven2. If you were to create a project from scratch, then Maven or Ant is probably the ideal way to go. If you use the NetBeans IDE, projects that you create will automatically contain an Ant build file (so that it can be built on all systems using Ant), but will add additional metadata so that it is recognized by NetBeans IDE. If you create a Maven project, either using Maven directly or using an IDE such as Eclipse or NetBeans, then that same project can be loaded in either NetBeans or Eclipse without any additional configuration changes (although you may need to install a plugin for Eclipse for it to recognize Maven projects; NetBeans recognizes Maven projects out of the box). If you are starting a project from scratch, you may be interested in the Java Project Template. If you are contributing to an existing project, how you view/edit the project depends on the build system chosen; if the project already uses Maven or Ant, loading it with other IDEs should be fairly simple, while if the project uses a specific IDE's quirks or uses some more exotic build system, it may be harder.

Categories