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
Propertiesat 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
*.battext 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).
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
Propertiesat 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
*.battext 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.
eclipse - How to run Java programs from the terminal? - Stack Overflow
eclipse - Run java with terminal - Stack Overflow
How to run an eclipse java project using the command line
Java - Build and run eclipse project from command line - Stack Overflow
Videos
If you are referencing any external libraries, then you have to add them to the classpath. You can add it during compilation of the classes this way.
Go to the src directory and :
javac -classpath ".:<path_to_jar_file>" teamL/*.java
TO execute :
java -cp ".:<path_to_jar_file>" teamL.<class_name>
if your are using eclipse, then go to <project_directory>/bin/ here you can find the compiled classes (so you dont have to compile them) and directly run them using the above java command
Note: Since your classes are packaged under teamL package, you have to run the classes from outside the package by specifying the fully qualified name like teamL.ServiceEndpoint
Have you added that jar to the classpath when you execute the program on the command line? e.g.
java -classpath location_of_jar ...
Hello,
I am making this java project for a class that I am taking, and the project has so many classes, packages, and some .jar libraries as well as using another project we made before. I always wondered how does eclipse compile and run all that mess and when I go to the directory after running in eclipse, I find no exe file or anything like that.
I want to be able to copy my folders and put them on another computer and compile and run them without eclipse being installed. Can someone please help me with that because I tried and I failed very hard.
I know that eclipse makes a folder of its own that stores settings and stuff, can I run my project on eclipse and then go to that folder and take some information from it and use it?
Thank you
Maven or Ant are the best option but for an Eclipse-only solution
you can choose File -> Export and select Java -> Runnable JAR File
then transfer the JAR file to your other machine and run this from the command line:
java -jar YOUR.JAR
You can run Java applications from the command line. The simplified syntax looks like this:
java -cp <classpath> <main class> <args>
where:
<classpath> - list of directories and/or JAR-files where needed classes reside separated by ";" for Windows or ":" for linux (default classpath is "." - the current directory);
<main class> - fully qualified name of the class containig main() method (for example, org.myself.HelloWorld)
<args> - various arguments for application if any.
So, if you find the directory where Eclipse stored compiled classes (usually it's bin) you may use the command, like
java -cp . my.package.MyClass
Or, if you use some libraries and classes in other directories, it could be:
java -cp some-cool-lib.jar:another-lib.jar:/some/directory/with/classes my.package.MyClass
cd into the directory in which your Calculator.java file is stored, run
javac Calculator.java
this will create a file Calculator.class. You can now run the compiled class with
java Calculator
mind that there is no .class to be added!
The online docs should be your first recourse: https://docs.oracle.com/javase/8/docs/technotes/tools/unix/java.html which tell us that the command line is
java [options] classname [args]
In more depth, and linked from somewhere on that page, you can read https://docs.oracle.com/javase/8/docs/technotes/tools/unix/classpath.html#CBHHCGFB
which is applicable to pretty much all the Java tools. These docs contain the answer to your question. You can either cd into the directory that is the root of your classpath and use the default classpath, as one answer suggested, or use the classpath options described in the docs to set the directory(-ies) at the top of your classpath. E.g.,
java -cp /Users/mac/Documents/workspace/Calculator/bin Calculator
It's important to note that you can't just run arbitrary Java code, but have to have some structure set up (for example, if you're going to call a method, what arguments are you passing in?) To run a specific piece of Java code, you should consider creating a main method in that class that just launches some specific piece of code under the constraints that you'd like. One way to do this would be to add a method public static void main(String[] args) containing the code you want to run. Once you've done this, you can right-click on it, choose the "Run as..." option, and select "Java application" to run the program you've written.
Alternatively, if you just want to check whether some specific piece of code is working, you could consider using JUnit to write unit tests for it. You could then execute the JUnit test suite from Eclipse to check whether that specific piece of code is functioning correctly. This is what I would suggest doing, as it makes it possible to test the software through every step of the development.
Hope this helps!
Yes - right-click it and choose Run as -> Java application. You just need a main method.
You can't compile & run just one file in Eclipse without the file being in a java project.
It is a very quick and easy process to create a Java Project in eclipse.
File -> New -> Java Project
You can use the default package (though it is not recommended) and put your single file in it and run it.
Try jGRASP, it's a really simple editor and compiler.