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
Related
I'm using JavaCompiler to compile .java code.
It works for me while running the class compiled using URLClassLoader to load remote jars.
But not work while compiling.
Tried to use options like
options.addAll(Arrays.asList("-classpath", "https://example.com/example.jar"));
Also tried to use customized JavaFileManager with URLClassLoader.
None of them works.
try below commands, and replace the .jar file with your external .jar file... into the command prompt or terminal.
$ javac -cp ".:./jars/common.jar" helloworld.java
$ java -cp ".:./jars/common.jar" helloworld
I know that there are similar posts to this, but I tried all suggested answers there and they didn't work out (maybe I did something wrong). I have a java project with the following folders:
bin
src
in the bin folder, I have few folders with .class files in them (compiled the project with eclipse, now just need to run it with command line). These are the folders inside bin:
client
game
protocol
threadPerClient
tokenizer
I also added the jar file (gson-2.2.1) to the bin folder. My main class is at threadPerClient folder, and this class name is MultipleClientProtocolServer. The main class get an int as a parameter. These are the commands that I tried to run via command line, from bin folder:
java -cp gson-2.2.1.jar threadPerClient.MultipleClientProtocolServer 4000.
And I got for that:
Error: Could not find or load main class threadPerClient.MultipleClientProtocolServer
java -cp threadPerClient/:./ MultipleClientProtocolServer 4000
And I got for that:
Error: Could not find or load main class MultipleClientProtocolServer
java -cp threadPerClient;. com.example.MultipleClientProtocolServer 4000
And I got for that:
Error: Could not find or load main class com.example.MultipleClientProtocolServer
What am I doing wrong?
Make sure you're running command 1. of your question from within the bin folder. Also make sure in -cp you specify the gson jar as well as the current folder:
Unix:
java -cp gson-2.2.1.jar:./ threadPerClient.MultipleClientProtocolServer 4000
Windows should be:
java -cp gson-2.2.1.jar;. threadPerClient.MultipleClientProtocolServer 4000
but I cannot test the command for windows since I don't have a windows machine.
I tried to run my basic HelloWorld.class file from my terminal.
I use the following input:
Java HelloWorld.class
But it says:
Error: Could not find or load "HelloWorld.class"
I have tried giving it a directory but it doesn't work.
because you didn't compiled or run it successufully.you should use
javac HelloWorld.java
to complile and
then use
java HelloWorld
to run it.
plz check this tutorial http://introcs.cs.princeton.edu/java/11hello/
The class should be (Executable Class should definitely contain the main method with same declaration as below)
Class MyClassName
{
// Methods here
public static void main (String args[])
{
// Code here
}
}
To Compile, it should be:
javac MyClassName.java
On successful compilation, MyClassName.class would be generated in your folder.
To run, it should be
java MyClassName
In case your java is in say D:/JavaWorkDir/src, You need to compile and run from the folder D:/JavaWorkDir/src. Also Ensure that your classpath is set appropiately.
You don't run it as
java HelloWorld.class
but
java HelloWorld
There is no need of .class extension.
But note you always have to use fully qualified name. So if your class resides in some package say myPackage then you need to run
java myPackage.HelloWorld
You are receiving this error because you shouldn't include the .class when you run the compiled file.
After you've compiled:
javac HelloWorld.java
run using:
java HelloWorld
(don't do: java HelloWorld.class)
Run it as Java HelloWorld and not like Java HelloWorld.class.
The error Error: Could not find or load "HelloWorld.class" is occuring because:
The class may have not compiled correctly.
The compiled class is not available on the path from where you are trying to run it.
Proper classpath is not set for compiling and running the java classes.
Whenever you write a Java Program named HelloWorld, you must compile it as:
javac HelloWorld.java
Once the HelloWorld.class class file generated in the same directory where you have your java file, compiled by the compiler, you can run it from console as:
java HelloWorld
if u want to try hello world you can also try to run it on the NetBeans application and also on Jdoodle.com
I want to add some jar files to my binary at runtime, but I keep getting errors. I'm running this on a Windows machine. My code is in a directory called SeleniumTest.
Here is the command I used to compile:
javac SeleniumTest\src\com\src\test\First.java -d SeleniumTest\bin -cp SeleniumTest\lib\junit-4.10.jar;SeleniumTest\lib\selenium-java-2.39.0.jar;SeleniumTest\lib\selenium-server-standalone-2.39.0.jar
This worked successfully. However when I try to run this command:
java -cp SeleniumTest\lib\junit-4.10.jar;SeleniumTest\lib\selenium-java-2.39.0.jar;SeleniumTest\lib\selenium-server-standalone-2.39.0.jar SeleniumTest\bin com.src.test.First
I get a message:
Error: Could not find or load main class SeleniumTest\bin
My code, First.java exists in
SeleniumTest\bin\com\src\test
What am I doing wrong?
try this
java -cp "SeleniumTest\lib\junit-4.10.jar;SeleniumTest\lib\selenium-java-2.39.0.jar;SeleniumTest\lib\selenium-server-standalone-2.39.0.jar;SeleniumTest\bin" com.src.test.First
try following
java -cp SeleniumTest\lib\junit-4.10.jar;SeleniumTest\lib\selenium-java-2.39.0.jar;SeleniumTest\lib\selenium-server-standalone-2.39.0.jar SeleniumTest\bin SeleniumTest\src\com\src\test\First
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