Compile and run Eclipse Project from command prompt - java

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.

Related

App.java is not on the classpath of project ss_d36b3017, it will not be compiled to a .class fileJava(32)

I am constantly facing this error and I have no idea how to fix it.
App.java is not on the classpath of project ss_d36b3017, it will not be compiled to a .class fileJava(32)
I am using Visual Studio Code for writing Java. I have set up the environment variable. javac --version and java --version and giving the expected results in PowerShell. I am using a very simple folder convention than is made by VS Code pallet. It has 1 folder (in drive D:)named project which has 3 folder named lib(This is vacant), src(This has the App.java)<readme.md It has no building tools. Code inside the App.java does not affect the issue.
I need some help.
Try the following two ways to see if question goes away.
Open Command Palette and choose Java: Clean Java Language Server Workspace
Right click the folder src and choose Add Folder to Java Source Path, then reload the window.

Compiling Java Program with .jar file and different packages

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!

Compile and export in java via Terminal

I have a Java program in Eclipse on Mac currently, and I normally have to use multiple clicks just to export my code into a .jar file to test on my server. I would like to automate the process via terminal.
Basically, I compile my code usually by selecting the project
Export as Runnable JAR file
Select library handling: extract required libarires into generated JAR
Select export destination and hit done.
How can I do this via terminal? I assume this would first require me to compile the Java file, then to convert it to jar is a whole another step.
Help would be much appreciated.
You can create shell script that does it. This technique is obsolete since ~1998. So, use one of popular build tools. If you are starting now take a look on Gradle. Although there are a lot of other tools: good old ant, maven, buildr, ivy etc.
You can script all these activities using a build script. Several libraries exist for this, but Apache Ant is a good place to start. Ant build scripts can be run from command line or within eclipse, and will do all compilation, packaging and (some) deployment for you with a single command.
http://ant.apache.org/
Create default entry point manifest file as in : http://docs.oracle.com/javase/tutorial/deployment/jar/appman.html
Open Terminal and create an executable jar file like this:
Sample Script:
#!/bin/bash
# set CLASSPATH if needed
cd workspace/src
javac -d . *.java
jar tf exported.jar .

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.

What will netbeans IDE do when I press Ctrl+F6 to run a java file?

I have no idea of how Netbeans IDE run a java file.
Firstly, it would ensure the .class file is up-to-date.
Then, execute the class. But from where (working directory) and with which command (parameter)?
I observe difference on how relative path is located when I run the java file from Netbeans IDE and when I run using Windows command prompt (i.e > java pack.age.name.ClassName)
You can find that out by putting this at the start of the main method of the class:
System.out.println(new File(".").getAbsolutePath());
It looks like it will run from the directory that the project is in (eg. ....\NetBeansProjects\JavaApplication1)
You cannot specify command line arguments for a single class (that I am aware of). To do that you have to use the Project | Properties (and there you can also set the working directory).
I would suggest that you do not write code that depends on the working directory if you can avoid it though...
You can get informative results by running the project's build.xml from the command line in verbose mode: ant -verbose run. Look for the [java] command options under run:. Typing ant -p will show you the available targets.

Categories

Resources