java -classpath ./sqljdbc4.jar myclassname - java

if I run java -classpath ./sqljdbc4.jar myclassname error is
Exception in thread "main" java.lang.NoClassDefFoundError:myclassname
if I run java myclassname error is
java.lang.ClassNotFoundException:
com.microsoft.sqlserver.jdbc.SQLServerDriver
It is on Linux. How to fix it?

The directory or jar containing your classes package tree (i.e. the directory containing the com directory, in the following example) must be in the classpath. And the sqljdbc4.jar must also be. Put both in the classpath:
java -cp ../classes:./sqljdbc4.jar com.foo.bar.MyClassName
Also note that you need to use the fully qualified name of the main class (as the above example shows), and that class names in Java use CamelCase by convention.

Related

Java could not find or load main class when I use -cp

I have a problem.
I try to run my java program with the library.
When I use
java com/myProg
it works.
But when I try to pass classpath it can't find or load main class
java -cp com/lib/lid.jar com/myProgram
error:
Error: Could not find or load main class myProgram
Caused by: java.lang.ClassNotFoundException: myProgram
Try specifying after the -cp option only the root of the working directory and don't include the package name there. If you're running the java command from the dir of /com 's location, -cp . com/myProgram will be enough,otherwise you'll need the fully qualified name after the classpath.
Not sure of the necessity of the .jar file, you stated that first you ran only via
java com/myProg.

I cant generate CUP parser and JFlex scanner in cmd

i'm new in compiler . i've read that i can generate xxx.flex file in cmd by this code :
java JFlex.Main xxx.flex
but i got this error :
Error: Could not find or load main class JFlex.Main
and for generating yyy.cup , i typed :
java java_cup.Main yyy.cup
but i got this error too :
Error: Could not find or load main class java_cup.Main
i confused ... what should i do ?
When you are invoking
java JFlex.Main xxx.flex
You ask java to load the JFlex.Main class and passing xxx.flex as parameter. As you do not tell java where is that JFlex.Main class, java is searching it in its classpath, and if you did not add the JFlex jar file to the class path, it results in the error message
Error: Could not find or load main class JFlex.Main
BTW, class names and namespaces are case-sensitive : in the JFlex jar file, the Main class is in the jflex directory, not JFlex so you need to invoke the jflex.Main class... unless you are using the a JFlex version prior to 1.5 where JFlex is legal.
To let java find the class:
either change the system class path (with the environment variable $CLASSPATH- or %CLASSPATH% in windows)
or just provide the location of the jar file the the java command with the -cp parameter
For example:
java -cp path/to/jflex-1.6.0.jar jflex.Main xxx.jflex
If the jar is in the current directory, you can just use
java -cp jflex-1.6.0.jar jflex.Main xxx.jflex
Or more simply, as it is an executable jar, you can omit the main class
java -jar jflex-1.6.0.jar xxx.jflex
Similarly, it seems that java is missing the CUP jar file while processing your yyy.cup file, you can fix it like for JFlex with
java -cp java-cup-11a.jar java_cup.Main yyy.cup
or
java -jar java-cup-11a.jar java_cup.Main yyy.cup

Basic Java - Packaging

I decided to post this question after trying to find an answer for it, and couldn't find one.
I'm studying for OCJP and tried few simple codes. This is what I did and need to do.
Created two .java sources, say TestOne.java, TestTwo.java [using
notepad++]
Created a directory named "package1" and placed the two sources in
them.
Both the source files have "package package1;" as their first statement.
TestOne.java has one public class and one class with default access.
TestTwo.java has one default class with an object of the default
class in TestOne.java.
The main method is in this default class in TestTwo.java. It tries to invoke a method in
the referred object that was created, using TestOne.java default class.
So after all that was set up compiled TestOne.java then TestTwo.java by setting the flag "classpath" in javac [ javac -classpath ]. Complied. But when I tried to run it gave me an exception "Exception in thread "main" java.lang.NoClassDefFoundError". Does anyone know what's wrong ?
run the code after compile
compile javac TestTwo.java
run after compile java TestTwo
try this
javac -d path cname.java
so write the code like this
javac -d c:\main testone.java javac -d c:\main testtwo.java
c:\main should exist in your pc
then while executing
java -cp path pn.classname
so whichever class contains main (say test2)
java -cp c:\main package1.testtwo

Run from command line, Wrong Name error

I want to run a Java project from the command line which I start using a batch file, but I get the wrong name error.
The directory setup:
srcMVC
bin (folder with .class files)
src (folder with .java files)
Batch file
Batch file:
set path=C:\Program Files\Java\jdk1.7.0_09\bin
javac src\model\*.java -d bin -cp src
javac src\controller\*.java -d bin -cp src
javac src\view\*.java -d bin -cp src
javac src\main\*.java -d bin -cp src
PAUSE
java bin\main.Main
PAUSE
Compiling works, but I get the following error:
Exception in thread "main" java.lang.NoClassDefFoundError: bin\main/Main (wrong name: main/Main)
Any suggestions?
package main;
// omitted imports
public class Main {
// omitted variables
public static void main(String[] args) {
// omitted implementation
}
}
The following statement resolved my error:
java -cp bin; main.Main
NoClassDefFoundError in Java comes when Java Virtual Machine is not able to find a particular class at runtime which was available during compile time.
For example if we have a method call from a class or accessing any static member of a Class and that class is not available during run-time then JVM will throw NoClassDefFoundError.
By default Java CLASSPATH points to current directory denoted by "." and it will look for any class only in current directory.
So, You need to add other paths to CLASSPATH at run time. Read more Setting the classpath
java -cp bin main.Main
where Main.class contains public static void main(String []arg)
you are wrongly exicuting java bin\main.main
main() is your main method but you should supply java interpreter the Class Name which implements main()
So if your class name is Test and file name is Test.java which has main() method
java Test
if your Test.java/Test class in is package my.test e.g - package com.my.test;
than, java com.my.test.Test
hope you got it !!
java bin/main.Main is wrong, you must specify -cp here:
java main.Main -cp bin
Here the first argument is the class name which should be found in the classpaths, rather than the class file location. And -cp just adds the logical path to classpaths. You should make the root of your project searchable in the classpath.
and for those javac commands, you have already specified the correct path, so you don't need -cp src. The difference here is the javac command uses logical path for .java files, while using java command you could only specify the path in -cp attribute.
You could also execute java main.Main without -cp if you enter the directory bin:
cd bin
java main.Main
Since the current path will be automatically be searched by java as a classpath.
Assuming you have a class called Main you have to run it with this command:
java bin\Main
It will call your main method.
Java run time (in your case the java.exe command), takes the class file name that containst the main() method as input. I guess you should be invoking it as "java bin\main" assuming there is a main.class which has a public static void main (String[]) method defined.
Note: General practice is to capitalize the first literal of any class name.

java compilation: classname Vs classname with file-extension

Running basic java programs from commands line is a 3 steps process:
Write code:
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World");
}
}
Compile by javac HellWorld.java which would check for errors & generate HelloWorld.class file.
run code by giving the class name --> java HelloWorld
Now,
I am curious to know why:
java HelloWorld works but when we give fullpath of the classfile, it throws an error
$ java HelloWorld.class
Error: Could not find or load main class HelloWorld.class
What does it make a difference if we give just the classname Vs classname with file-extension?
What does it make a difference if we give just the classname Vs classname with file-extension?
The argument you give to the java binary isn't meant to be a filename. It's meant to be a class name. So in particular, if you're trying to start a class called Baz in package foo.bar you would run:
java foo.bar.Baz
So similarly, if you try to run java HelloWorld.class it's as if you're trying to run a class called class in a package HelloWorld, which is incorrect.
Basically, you shouldn't view the argument as a filename - you should view it as a fully-qualified class name. Heck there may not even be a simple Baz.class file on the file system - it may be hidden away within a jar file.
What does it make a difference if we give just the classname Vs classname with file-extension?
It is because that is the way it is. Sun / Oracle have implemented the java command to work that way since Java 1.0, and changing it would be massively disruptive.
As Jon says, the argument to the command is a fully qualified class name, not a filename. In fact, it is quite possible that a file with the name HelloWorld.class does not exist. It could be a member of a JAR file ... or in some circumstances, just about anything.
In Java 11 and later it is also possible to compile and run a single Java source file with a single command as follows:
java HelloWorld.java
(This possible because Oracle no longer supports Java distributions without a Java bytecode compiler.)
In the Java programming language, source files (.java files) are compiled into (virtual) machine-readable class files which have a .class extension.
When you run java class file after compile then run the following command:
java HelloWorld
Note: Need to setup java classpath

Categories