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
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 ...
Simply navigate to the directory that the class file exists in and use
java -classpath . myClass
Edit: You can replace the . with any classpath. For example, to find your classpath you can use echo %CLASSPATH%
Edit: Looks like there's quite a bit of information that might help you here.
How to run a java project from command line using Runnable jar
Using Eclipse you can easily run a java program but using Runnable jar is slightly different. Steps to run a java project:
- Export the java project in to a Runnable jar - using Eclipse IDE
- Select the main or running class file - Launch configuration
- In Library handling - select the option [ Extract required libraries in to jar file ]
- Open command prompt go to the directory where runnable jar is available
- type > java -jar Runnable.jar
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.
/*
* put this in a file named CommandLineExample.java
*
*/
class CommandLineExample
{
public static void main ( String [] arguments )
{
System.out.println("Hello, world");
}
}
type the following from the command line to compile it:
$ javac CommandLineExample.java
then you can run it from the command line like this:
$ java CommandLineExample
I would recommend using a variant of Eclipse Java IDE when working with jar development.
File > New > Java Project
Right Click "src" > New > Package > Name your package pack1.projectname.
Right Click "pack1.projectname" > New > Class > projectname as class name.
Write all the code your heart desires for this project. Java is a very interesting language. Always use packages with your class files, especially when you are sure that you will be importing your own code in other files. So you can do import pack1.projectname.handler.EventHandler;
File > Export > Java > JAR file, or Runnable JAR file > Next > export destination
This will package your jar file, appropriately with a META-INF manifest, also if you are developing an application or plugin, for anything like Minecraft. You will want a plugin.yml and config.yml file. You right click the project folder not "src" in the project explorer. Do New > File > plugin.yml and config.yml. Just a side note.
The reason I always use pack1 as my toplevel package identifier is of course so I can tell which pack I am working with as I update my project. I always do this to easily compare files I have updated, why I updated them, etc. etc. So initial distribution would be pack1.projectname. Next version pack2.projectname. Also, when not updating code in an entire file, you can just import pack1.projectname.handler.EventHandler; import pack2.projectname.builder.RandomFile; pack3.projectname.init.init; These is a very organized way to develop in Java using Eclipse.
Hope this helped you on your quest to beginning your projects in Java. I will not write Java code on stackoverflow unless the person asking shows a snippet of his own project, and just cannot break through his coders block. Practice makes perfect.
Perform the following steps:
- Create a new project in eclipse
- Link your classes from the project properties' buildpath
- Go to Run->Run Configurations
- Create a new configration
- Point your main class
- And Run
if you are just starting learning Java and dont understand whats going on with all that stuff with eclipse, I would recommend you to start from the basics, write your code in an ordinary text editor (notepad will do, or notepad++ if you want something more), after you have written and saved your source file, open up your command prompt, navigate to the location of the file, and run:
javac YourFileName.java
java YourFileName
here is more detailed guide:http://introcs.cs.princeton.edu/java/15inout/windows-cmd.html
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