compile and run java code written on Eclipse on cmd prompt - java

I am trying to run a java project that includes two libraries jpbc-api-1.2.1.jar and jpbc-plaf-1.2.1.jar. The code has been written on Eclipse and I am now trying to run it on the cmd prompt on a Windows machine. I went to the .classpath file and made sure to modify the paths as follows:
classpathentry kind="lib" path="lib/jpbc-api-1.2.1.jar" sourcepath="lib/jpbc-api-1.2.1.jar"
classpathentry kind="lib" path="lib/jpbc-plaf-1.2.1.jar" sourcepath="jpbc-plaf-1.2.1.jar"
Whenever I try to compile my java code I get the following error:
error: cannot find symbol
To compile I used the command:
javac filename.java
That is obviously because the compiler cannot locate the files I am trying to show the path to. Is there a specific way to compile and run the code?

You don't need the compiler to run the program if it's already compiled by Eclipse - you only need the Java runtime. The .classpath file is Eclipse-specific, so it won't be used by either the Java compiler (javac) or the Java runtime (java).
Assuming that your main class is called com.my.MainClass and your classes directory is called "myclasses", you would run your class using this command line:
java -classpath lib/jpbc-api-1.2.1.jar;lib/jpbc-plaf-1.2.1.jar;myclasses com.my.MainClass
I think you should have a look at the documentation of the Java command line tools:
http://docs.oracle.com/javase/7/docs/technotes/tools/windows/java.html
http://docs.oracle.com/javase/7/docs/technotes/tools/windows/javac.html
Good luck!

Related

Javac Compiles Program, but Can't Run with Java

I've never had issues with compiling with javac and running with java before, but here we are.
The program, MainApp.java, depends on a jar file and a few other programs, so I ran the following (which ran without issue):
javac -cp gson.jar *.java
However, when it comes time to run it, I can't seem to get it to work. I've tried:
java -cp gson.jar MainApp.java
but it complains that it can't find the other java files.
I then tried listing the file it said it was missing:
java -cp gson.jar RepositoryData.java MainApp.java
but it gave the same error.
I then tried listing all the dependencies:
java -cp gson.jar Comment.java Issue.java Owner.java Repository.java RepositoryData.java MainApp.java
but it complains that it can't find a main function in the Comment.java file - this leads me to think I'm going about this the wrong way, and I probably shouldn't be directly referencing dependencies in the java call.
I also know that the program does work, as I've compiled it using an editor without issue, but I'm required to do this via the command line for a class.
How should I be properly calling it?

About java compiling

I programmed two java classes (Coordinate.java and Orienteering.java) using Eclipse. These classes are stored in C:\Users\Jack\Desktop\Orienteering\Orienteering\src\jp\co\worksap\global
The package is jp.co.worksap.global.Orienteering. I want to know how can I compile the two files in Windows command line and run it? Orienteering.java is the main class.
First open the command line, then change to the source folder
cd C:\Users\Jack\Desktop\Orienteering\Orienteering\src\jp\co\worksap\global
javac -g Orienteering.java Coordinate.java
That should generate two corresponding .class files (with debug symbols). Then move up a few directories and execute java like,
cd C:\Users\Jack\Desktop\Orienteering\Orienteering\src\
java -cp . jp.co.worksap.global.Orienteering
note alternatively, the javac line could have been javac -O *.java (which would not include debug symbols and would optimize the build, and would compile any other java source files). Also, it is more typical to use a tool like ant, maven, gradle or sbt for java builds.
In Eclipse there is an option to compile it for you. I believe it's:
File -> Export -> Runnable Jar File -> [Select the classes] -> Select Location -> Run the JAR File that gets created in that location.
As far as I know, I think Windows CMD can only compile one Java Class.
Try this:
Goto your source directory using cd command:
cd C:\Users\Jack\Desktop\Orienteering\Orienteering\src
Compile your classes using javac
javac jp\co\worksap\global\Coordinate.java jp\co\worksap\global\Orienteering.java
To run your program:
java -cp . jp.co.worksap.global.Orienteering

Could not find or load main class Java Error Notepad++

I've implemented "Java Compile" and "Compile and Run" (the second as Java Compile and Run) as described in this answer: Java compile and run using notepad++ and nppexec.
Note: I'm using 1.8.0_20 rather than 1.7.0 as described in the answer.
However, when I try to run HelloWorld (as found here: http://docs.oracle.com/javase/8/javafx/get-started-tutorial/hello_world.htm) to test the compiler, I get the following response, including a "Could not find or load main class" error:
NPP_EXEC: "Java Compile and Run"
CD: C:\Users\Bova\Documents
Current directory: C:\Users\Bova\Documents
"C:\Program Files (x86)\Java\jdk1.8.0_20\bin\java" -classpath "C:\Users\Bova\Documents" "HelloWorld"
Process started >>>
Error: Could not find or load main class HelloWorld
<<< Process finished. (Exit code 1)
================ READY ================
What do I need to change to avoid this error?
I had this problem in Notepad++ too. What I did to fix it was I went to the Plugins menu, clicked on NppExec, and selected Follow $(CURRENT_DIRECTORY). After that I can run programs just fine.
The very first line of HelloWorld.java reads:
package helloworld;
Java packages are mapped to directories on the filesystem, so the interpreter expects to find HelloWorld.class inside a helloworld directory. Move the .class file to a helloworld subdir and run it as:
> java helloworld.HelloWorld
from the parent directory (i.e. the directory which contains helloworld/).

How to run Java program in command prompt

I created a Java project to call a Web service.
It has one Main java file and another class file.
I have used some jar files for HTTP client.
In Eclipse it runs fine.
I need to run the Java program in command prompt by passing some arguments.
In command prompt I went to src folder containing main java and sub class java file and gave the following command
javac mainjava.java
I'm getting following error
mainjava.java:14: cannot find symbol
symbol : class SubClass
here SubClass is my another java class file used to call the web service.
How to run the program by passing arguments?
javac is the Java compiler. java is the JVM and what you use to execute a Java program. You do not execute .java files, they are just source files.
Presumably there is .jar somewhere (or a directory containing .class files) that is the product of building it in Eclipse:
java/src/com/mypackage/Main.java
java/classes/com/mypackage/Main.class
java/lib/mypackage.jar
From directory java execute:
java -cp lib/mypackage.jar Main arg1 arg2
A very general command prompt how to for java is
javac mainjava.java
java mainjava
You'll very often see people doing
javac *.java
java mainjava
As for the subclass problem that's probably occurring because a path is missing from your class path, the -c flag I believe is used to set that.
You can use javac *.java command to compile all you java sources. Also you should learn a little about classpath because it seems that you should set appropriate classpath for succesful compilation (because your IDE use some libraries for building WebService clients). Also I can recommend you to check wich command your IDE use to build your project.
All you need to do is:
Build the mainjava class using the class path if any (optional)
javac *.java [ -cp "wb.jar;"]
Create Manifest.txt file with content is:
Main-Class: mainjava
Package the jar file for mainjava class
jar cfm mainjava.jar Manifest.txt *.class
Then you can run this .jar file from cmd with class path (optional) and put arguments for it.
java [-cp "wb.jar;"] mainjava arg0 arg1
HTH.
javac only compiles the code. You need to use java command to run the code. The error is because your classpath doesn't contain the class Subclass iwhen you tried to compile it. you need to add them with the -cp variable in javac command
java -cp classpath-entries mainjava arg1 arg2 should run your code with 2 arguments

Problems compiling and running Java app with Bluecove (NoClassDefFoundError)

I have this app that uses bluetooth, so I need both, bluecove and bluecove-gpl packages, when I run it in NetBeans I have no problem at all, and works perfectly fine. But I still can't compile and run from the command line (Ubuntu 11.04).
I'm using this line for compilation:
$ javac -Xlint:unchecked -classpath bluecove-2.1.0.jar:bluecove-gpl-2.1.0.jar Client.java
And it doesn't return errors and it generates a .class file
Then I try to run the .class file like this:
java -classpath bluecove-2.1.0.jar:bluecove-gpl-2.1.0.jar Client
But it returns a NoClassDefFoundError.
Could not find the main class: SPPClient.
Why is this happening?
You probably need to add your current directory (or whatever directory your class files reside in) to the class path.
Try something like
java -classpath .:bluecove-2.1.0.jar:bluecove-gpl-2.1.0.jar Client
or
java -classpath bin:bluecove-2.1.0.jar:bluecove-gpl-2.1.0.jar Client
You need to have the main class definition in the manifest file:
Main-Class: classname

Categories

Resources