I have an ant script which compiles java code and executes it but when I run it on a machine where ant is not installed it does not execute nor compile. Is there any way to do this?
Thanks
No, there is no way to do this.
Basically, running a program require having this program installed. No mater what program it is.
Copy bin and lib directories from a Ant -package to your project path and run "bin\ant".
Or for example you project path could contain:
build.xml
src
software\ant\bin
software\ant\lib
You can run "software\ant\bin\ant" in you project path.
You can try to create something similar to the gradle wrapper for ant.
Basically, it's a batch/shell script and a simple jar that only need a valid JAVA_HOME to run. When you launch this script it download gradle if required (i.e. not already available at a well known location), then it execute your gradle build.
Imagine someone saying, "I have a C/Python/Perl/C++/Whatever program I want to compile. Is there anyway to compile that C/Python/Perl/C++/Whatever program without having to install C/Python/Perl/C++/Whatever?"
The answer would pretty much be no. You need Ant to interpret the Ant build files.
Fortunately, installing Ant is pretty straight forward. You download the ZIP file from Ant's Distribution Page, then unzip it into some directory (preferably one without spaces in the name. C:\Program Files\Apache Ant isn't a good choice, but C:\apache-ant would be fine.
Now, you set two environment variables, ANT_HOME pointing to where you installed Ant, and JAVA_HOME pointing where you installed the Java JDK. (Windows comes with the Javaruntime, but you need to have the Java JDK which you can get from Oracle.) This can be done by going into the System Control Panel->Advanced
WARNING: When installing Java Developer Kit (JDK), be very, very careful not to accidentally install the Ask Toolbar. Java does this by default.
Once that is done, set your PATH (again via the PATH Environment variable to include %ANT_HOME/bin and %JAVA_HOME%/bin at the beginning of your path.
Then, running ant on the command line in a Console window will run Ant. The whole procedure takes about 10 minutes to do.
Related
I have the following task at hand: I have a Java project in Eclipse on machineA that I need to run on machineB. These machines are similar but different and I am getting an error trying to run on B the JAR that I exported on A. So what I want to do it to try rebuilding my code on B. Now, a standard way to do that would be to set up the project on A as an Ant or Maven build, copy the project to B and then run Ant/Maven on B from command line (I don't have Eclipse installed on B). However, B is a bit... "special" in different ways. For instance, its default java is 1.6 but it also has a local install of 1.7 that I am using. That install has both java and javac. There is no Maven there at all and there is a custom Ant install that may not correspond to the java install.
So the question is: is there a way to generate the javac command that would be used on A to export to JAR, so that I could run that command from the command line, possibly with some mods, to (re)build the project on B?
UPDATE: Would it be easier to do the same in Netbeans? If so, I would be willing to give it a try.
I think what you want is the jar command, as opposed to a javac option. The jar command, part of the Java JDK, creates a jar file for you. There is documentation here.
But this will create the same jar that eclipse will create, so I expect there is some problem running it on your B machine that does not have to do, strictly, with creating jar files. So more detail on the problem you're having on B would help.
Hey I downloaded the latest version of eclipse and extracted the folder and ran it using ./eclipse everything worked fine. I was wondering if there is a way to just run eclipse or eclipse & from the terminal and eclipse will execute without going to the eclipse directory. I did it before when I used sudo apt-get to install eclipse but they don't have the latest version of eclipse. I am using linux btw and I have to navigate to the folder where I put eclipse every time I want to run it. If there is a obvious answer to the question I apologize for asking but I couldn't find anything on it.
Thank you for any input.
Man, is normal that way to execute eclipse, eclipse does not install, is like a executable. You need use ./eclipse always and its normal you need go to directory.
Why dont create a desktop launcer like here
If you are using bash there is a file called .bashrc, enter the following commands:
$ cd ~
$ ls -a
If you look, you should see .bashrc, then you need to edit it using some text editor like nano:
$ nano .bashrc
Once inside nano, you need to create an alias. An alias is a global variable in bash that references another value, for instance:
alias eclipse="/path/to/executable/eclipse"
This is create a variable called eclipse that when typed into the terminal, in any directory, will execute and run eclipse. So simply add that line, replacing /path/to/executable/eclipse with the actual path of the program.
Once that is done you can type eclipse anywhere in bash and it will work.
SIDE NOTE:
If you don't want your terminal session to hang when you open eclipse, type an ampersand after eclipse:
$ eclipse &
This will run the program in the background, freeing up your terminal.
You can edit your .bashrc file and modify the PATH environment variable to include the path where you installed Eclipse.
Also, you should consider adding a desktop launcher which you can just double-click to run Eclipse.
I can't open or run my .jar file.
I just installed java, but I tried to open the .jar with other programs first, so the double-click defaults to something else and I can't change it back.
java -jar myfile.jar`
Above command returns:
'java' is not recognized as an internal or external command, operable program or batch file.
Is there a way I can still open/run this?
e: OS is Windows 8.
Also, I downloaded the .jar file; didn't create it myself (if that's relevant)
Not sure if it contains an executable (but I think it does).
you can use the command prompt:
javaw.exe -jar yourfile.jar
Hope it works for you.
There are two different types of Java to download: The JDK, which is used to write Java programs, and the RE (runtime environment), which is used to actually run Java programs. Are you sure that you installed the RE instead of the SDK?
Use cmd prompt and type
java -jar exapmple.jar
To run your jar file.
for more information refer to this link it describes how to properly open the jar file.
https://superuser.com/questions/745112/how-do-i-run-a-jar-file-without-installing-java
You may have several JDKs installed in your PC. Some older JDK installers also copy some java files such as java.exe, javaw.exe into C:\Windows\System32 folder.
I had a similar issue, and searched the internet for a solution and none of the suggestions didn’t open by double clicking the .jar file.
In my case the reason is I have multiple JDK & JRE versions installed on my computer. Since I am a software developer working with several different versions for different clients I need to use multiple JDKs in my PC (Windows 10 Pro). So I do not want to change the system variables (i.e. JAVA_HOME, JRE_HOME or PATH), instead I use command prompt to run java in user process whenever I wanted to use a different version.
When installing JDK it registers the .jar file association with latest version we installed in the PC. If you right click on the .jar icon and select properties, it will show that file opens with “Java(TM) Platform SE Binary”. If we look at the registry key: HKEY_CLASSES_ROOT\jarfile\shell\open\command, it will point to latest JDK version.
It is not a good idea (sometimes annoying) to change the registry key every time I want to run an app build from a different version.
So in my situation it is impossible to just double click the .jar file to execute it. But instead I found a work around solution myself.
Scenario:
Multiple JDKs (1.7, 1.8, 9.0, 10.0, 11.0, and 12.0)are installed in the PC, so the latest installed was 12.0.
Problem
Want to double click an executable .jar developed using JDK 1.8 and didn’t work
This is my work around solution:
Create a shortcut for the .jar file that you want to open.
Right click the shortcut icon and select properties -> Shortcut tab
Change the text in the target (for example "D:\Dev\JavaApp1.8.jar")
To
"C:\Program Files\Java\jdk1.8.0\bin\javaw.exe" -jar
"D:\Dev\JavaApp1.8.jar"
Then click ok Double click the shortcut.
It should now open the app.
I was having this same issue for both Windows 8 and Windows Server 2012 configurations.
I had installed the latest version of JDK Java 7 and had set my **JAVA_HOME**system env variable to the jre folder: *C:\Program Files (x86)\Java\jre7*
I also added the bin folder to my **Path** system env variable: *%JAVA_HOME%\bin*
But I was still having problems with double clicking the executable jar files. I found another system env variable OPENDS_JAVA_ARGS that can be used to set the optional properties for javaw.exe. So I added this variable and set it to: -jar
Now I am able to run the executable jar files when double clicking them.
In cmd you can use the following:
c:\your directory\your folder\build>java -jar yourFile.jar
However, you need to create you .jar file on your project if you use Netbeans. How just go to Run ->Clean and Build Project(your project name)
Also make sure you project properties Build->Packing has a yourFile.jar
and check Build JAR after Compiling
check Copy Depentent Libraries
Warning: Make sure your Environmental variables for Java are properly set.
Old way to compile and run a Java File from the command prompt (cmd)
Compiling: c:\>javac Myclass.java
Running: c:\>java com.myPackage.Myclass
I hope this info help.
Go to your java directory,
Copy this path
C:\Program Files\Java\jdk1.8.0_40\bin
Right click on my computer , click properties, then go to "Advanced system settings"
click , Environment variables.
go to "System variables" table, find an entry named "path".
Double click it and go to the end, put a semicolon and paste your path, apply and ok.
It should run now.
first of all, we have to make sure that you have downloaded and installed the JDK.
In order to download it click on the following link
http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html
(Do not forget to check "Accept License Agreement", before you choose the version you want to download)
For Windows OS 32-Bit (x86) choose "jdk-8u77-windows-i586.exe"
For Windows OS 64-Bit (x64) choose "jdk-8u77-windows-x64.exe"
Install the file that is going to be downloaded. During the installation, pay attention, because you have to keep the installation path.
When you have done so, the last thing to do, is to define two "Environment Variables".
The first "Environmental Variable" name should be:
JAVA_HOME
and its value should be the installation path
(for example: C:\Program Files\Java\jdk1.8.0_77)
The second "Environmental Variable" name should be:
JRE_HOME and its value should be the installation path
(for example C:\Program Files\Java\jre8)
As soon as you have defined the Environment Variables, you can go to command prompt (cdm) and run from every path your preferred "java.exe" commands. Your command line can now recognize your "java.exe" commands.
:)
P.S.: In order to define "Environment Variable", make a right click on "This PC" and select "properties" from the menu. Then the "System" window will appear and you have to click on "Advanced system settings". As a consequence "System properties" window shows. Select the "Advanced" tab and click on "Environment Variables" button. You can now define the aforementioned variables and you're done
You must create a manifest file and specify your class that has the main method. you can build your jar file with manifest file as a parameter.
jar cfm MyJar.jar Manifest.txt MyPackage/*.class
Manifest-Version: 1.0
Archiver-Version: Plexus Archiver
Created-By: Apache Maven
Built-By: Cakes
Build-Jdk: 1.6.0_04
Main-Class: com.foo.App
An easy way to execute .jar files is to create a batch file.
Let's say you placed your jar file on your Desktop;
#echo OFF
java -jar C:\Users\YourName\Desktop\myjar.jar
Copy this code to a .txt file, modify "YourName" and save as "myjar.bat". Then whenever you double click, the jar file will be executed.
Hope this helps.
Short trick: after I only REMOVED SPACES from names of the folders, where the .jar file was, double-clicked worked and the file executed.
In Netbeans please delete current you create the jar file and on the project explore of Netbeans please clean and build 2 or 3 times and right the project folder in project explore and build the jar file.
I had this problem a while back and the solution was really easy.
Just uninstall the current version of Java, download an older one, then uninstall the older and install the latest again.
For example: Java 8 Update 73 current install Java 7 Update 95.
How it works: Java's registry keys were messed up, and when you install the older version they get fixed.
If the intention of the question is to view the contents of the JAR file, then the following java command would help.. (provided, JDK location is added to the environment variables.)
Windows Command prompt> jar tvf yourJarFile.jar
Example:
jar tvf log4j-extras-1.2.17.jar
Reference: http://docs.oracle.com/javase/tutorial/deployment/jar/view.html
I downloaded the latest JDK 7u10. Once you do that, try running your jar, It should execute.
I am trying to compile an application from the command line
The programme compiles and runs fine in eclipse, howvever when i goto the command line and use javac to compile my java file i get 23 errors, the majority of which are Cannot Find Symbol, with an arrow pointing to the . in a package name.
Does anyone have any ideas on what i need to do differently?
Thanks
Your classpath is not set up correctly. Look at your Eclipse project in the .classpath file. In there you will find a lot of classpathentry elements. You will need to replicate this for your command line compilation.
To do this manually you must first set your CLASSPATH environment variable to a list of directories (or jar files) containing class definitions.
You can also use a build tool called ant to automate this for you.
I advise against setting the classpath as an environment variable because it is too intrusive (all your java programs will see it).
A command line for compiling a Java app which dependes on Log4j might look like this:
javac -cp C:\dev\mvn\repo\log4j\log4j\1.2.16\log4j-1.2.16.jar AppenderTester.java
If you have multiple classpath entries you need to separate them with a semicolon.
For ease of use you could create a startup script. This can be a simple batch script or a more elaborate ant script (which requires installing ant).
This is only the tip of the iceberg known as 'classpath hell'.
EDIT: you can also take a look at the Eclipse feature 'export runnable JAR', which packs your application together with all its dependencies in a JAR file.
Is there a way to compile an Eclipse-based Java project from the command line?
I'm trying to automate my build (using FinalBuilder not ant), and I'm neither a Java nor Eclipse expert. I can probably figure out how to do this with straight java command line options, but then the Eclipse project feels like a lot of wasted effort.
In the event that there is no way to compile an Eclipse project via the command line, is there a way to generate the required java command line from within Eclipse? Or are there some files I can poke around to find the compile steps it is doing behind the scenes?
Guys, I'm looking for an answer that does NOT include ant. Let me re-iterate the original question ....... Is there a way to build an Eclipse project from the command line?
I don't think this is an unreasonable question given that I can do something like this for visual studio:
devenv.exe /build "Debug|Any CPU" "C:\Projects\MyProject\source\MyProject.sln"
You can build an eclipse project via a workspace from the command line:
eclipsec.exe -noSplash -data "D:\Source\MyProject\workspace" -application org.eclipse.jdt.apt.core.aptBuild
It uses the jdt apt plugin to build your workspace automatically. This is also known as a 'Headless Build'. Damn hard to figure out. If you're not using a win32 exe, you can try this:
java -cp startup.jar -noSplash -data "D:\Source\MyProject\workspace" -application org.eclipse.jdt.apt.core.aptBuild
Update
Several years ago eclipse replaced startup.jar with the "equinox launcher"
https://wiki.eclipse.org/Equinox_Launcher
On Eclipse Mars (MacOX):
java -jar /Applications/Eclipse.app/Contents/Eclipse/plugins/org.eclipse.equinox.launcher_1.3.100.v20150511-1540.jar -noSplash -data "workspace" -application org.eclipse.jdt.apt.core.aptBuild
The -data parameter specifies the location of your workspace.
The version number for the equinox launcher will depend on what version of eclipse you have.
To complete André's answer, an ant solution could be like the one described in Emacs, JDEE, Ant, and the Eclipse Java Compiler, as in:
<javac
srcdir="${src}"
destdir="${build.dir}/classes">
<compilerarg
compiler="org.eclipse.jdt.core.JDTCompilerAdapter"
line="-warn:+unused -Xemacs"/>
<classpath refid="compile.classpath" />
</javac>
The compilerarg element also allows you to pass in additional command line args to the eclipse compiler.
You can find a full ant script example here which would be invoked in a command line with:
java -cp C:/eclipse-SDK-3.4-win32/eclipse/plugins/org.eclipse.equinox.launcher_1.0.100.v20080509-1800.jar org.eclipse.core.launcher.Main -data "C:\Documents and Settings\Administrator\workspace" -application org.eclipse.ant.core.antRunner -buildfile build.xml -verbose
BUT all that involves ant, which is not what Keith is after.
For a batch compilation, please refer to Compiling Java code, especially the section "Using the batch compiler"
The batch compiler class is located in the JDT Core plug-in. The name of the class is org.eclipse.jdt.compiler.batch.BatchCompiler. It is packaged into plugins/org.eclipse.jdt.core_3.4.0..jar. Since 3.2, it is also available as a separate download. The name of the file is ecj.jar.
Since 3.3, this jar also contains the support for jsr199 (Compiler API) and the support for jsr269 (Annotation processing). In order to use the annotations processing support, a 1.6 VM is required.
Running the batch compiler From the command line would give
java -jar org.eclipse.jdt.core_3.4.0<qualifier>.jar -classpath rt.jar A.java
or:
java -jar ecj.jar -classpath rt.jar A.java
All java compilation options are detailed in that section as well.
The difference with the Visual Studio command line compilation feature is that Eclipse does not seem to directly read its .project and .classpath in a command-line argument. You have to report all information contained in the .project and .classpath in various command-line options in order to achieve the very same compilation result.
So, then short answer is: "yes, Eclipse kind of does." ;)
After 27 years, I too, am uncomfortable developing in an IDE. I tried these suggestions (above) - and probably just didn't follow everything right -- so I did a web-search and found what worked for me at 'http://incise.org/android-development-on-the-command-line.html'.
The answer seemed to be a combination of all the answers above (please tell me if I'm wrong and accept my apologies if so).
As mentioned above, eclipse/adt does not create the necessary ant files. In order to compile without eclipse IDE (and without creating ant scripts):
1) Generate build.xml in your top level directory:
android list targets (to get target id used below)
android update project --target target_id --name project_name --path top_level_directory
** my sample project had a target_id of 1 and a project name of 't1', and
I am building from the top level directory of project
my command line looks like android update project --target 1 --name t1 --path `pwd`
2) Next I compile the project. I was a little confused by the request to not use 'ant'.
Hopefully -- requester meant that he didn't want to write any ant scripts. I say this
because the next step is to compile the application using ant
ant target
this confused me a little bit, because i thought they were talking about the
android device, but they're not. It's the mode (debug/release)
my command line looks like ant debug
3) To install the apk onto the device I had to use ant again:
ant target install
** my command line looked like ant debug install
4) To run the project on my android phone I use adb.
adb shell 'am start -n your.project.name/.activity'
** Again there was some confusion as to what exactly I had to use for project
My command line looked like adb shell 'am start -n com.example.t1/.MainActivity'
I also found that if you type 'adb shell' you get put to a cli shell interface
where you can do just about anything from there.
3A) A side note: To view the log from device use:
adb logcat
3B) A second side note: The link mentioned above also includes instructions for building the entire project from the command.
Hopefully, this will help with the question. I know I was really happy to find anything about this topic here.
The normal apporoach works the other way around: You create your build based upon maven or ant and then use integrations for your IDE of choice so that you are independent from it, which is esp. important when you try to bring new team members up to speed or use a contious integration server for automated builds. I recommend to use maven and let it do the heavy lifting for you. Create a pom file and generate the eclipse project via mvn eclipse:eclipse. HTH
This question contains some useful links on headless builds, but they are mostly geared towards building plugins. I'm not sure how much of it can be applied to pure Java projects.
Just wanted to add my two cents to this. I tried doing as #Kieveli suggested for non win32 (repeated below) but it didn't work for me (on CentOS with Eclipse: Luna):
java -cp startup.jar -noSplash -data "D:\Source\MyProject\workspace" -application org.eclipse.jdt.apt.core.aptBuild
On my particular setup on CentOS using Eclipse (Luna) this worked:
$ECLIPSE_HOME/eclipse -nosplash -application org.eclipse.jdt.apt.core.aptBuild startup.jar -data ~/workspace
The output should look something like this:
Building workspace
Building '/RemoteSystemsTempFiles'
Building '/test'
Invoking 'Java Builder' on '/test'.
Cleaning output folder for test
Build done
Building workspace
Building '/RemoteSystemsTempFiles'
Building '/test'
Invoking 'Java Builder' on '/test'.
Preparing to build test
Cleaning output folder for test
Copying resources to the output folder
Analyzing sources
Compiling test/src/com/company/test/tool
Build done
Not quite sure why it apparently did it twice, but it seems to work.
I wanted to just add this comment to this question for anyone who may encounter this issue in the future.
Once, I was working on a project and building complex and inter-related JAVA projects from CMD (using ECLIPSE) was my only option. So, all you need to is this:
Download your desired ECLIPSE IDE (tested it with ECLIPSE OXYGEN, Eclipse IDE for Enterprise Java and Web Developers and Eclipse IDE for Java Developers and it works fine)
Open your project in the ECLIPSE and change the ECLIPSE configuration based on your project
Close the ECLIPSE and save the .metadata created from your ECLIPSE (If your workspace is going to be refreshed in the future)
Run this script in the CMD:
"%ECLIPSE_IDE%\eclipsec.exe" -noSplash -data "YOUR_WORKSPACE" -application org.eclipse.jdt.apt.core.aptBuild
So, all I wanted to say is that, I could not build using the above commands without the .metadata being in the workspace. Make sure it exists there. If you need, make a copy of .metadata and copy-paste it each time your want to execute the commands.
Hi Just addition to VonC comments. I am using ecj compiler to compile my project. it was throwing expcetion that some of the classes are not found. But the project was bulding fine with javac compiler.
So just I added the classes into the classpath(which we have to pass as argument) and now its working fine... :)
Kulbir Singh
Short answer. No.
Eclipse does not have a command line switch like Visual Studio to build a project.