Is there any limitation on the length of the classpath in java? - java

I tried to set classpath from command line for a java class
java -cp lib1.jar;lib2.jar;lib3.jar MyProgram
The above code threw runtime exception as it has further dependency with other jar file when I added the jar file it is unable to recognize the Main class in MyProgram class and threw
Error:Main method not found in class MyProgram,please define the method as public static void main(Stirng[] args)
The length of the strings lib1 ,lib2 and lib3 is about 400 characters.
Is there any limitation on the size of the classpath which can be set from command line .I tried setting class path using Manifest file as well but still it is throwing same exception.

I cannot test the program that you have written,so one solution by observing the details provided could be-
java –classpath ${CLASSPATH} MyProgram
Note: here you would have to set CLASSPATH variable before using it.

Related

How to include a third-party jar-file in compilation and runtime using command prompt in Windows 10?

I'm trying to understand the inclusion of third party jar files in a java project using only the command line in Windows 10.
Specifically, I try to include the file json-20200518.jar in my "project" so that I can use the java object JSONObject in the project.
My java file:
package com.mypackage.example;
import org.json.JSONObject;
class Example {
public static void main(String[] args) {
// ... program logic
}
}
location of my java file (Examp.java):
./com/mypackage/example
location of jar file:
./jars
using cmd win10 I compile:
javac -cp "C:\Users\pfort\Desktop\java\jars\json-20200518.jar" "C:\Users\pfort\Desktop\java\com\mypackage\example\Examp.java"
compilation is successful.
Run:
java -cp "C:\Users\pfort\Desktop\java\jars\json-20200518.jar" com.mypackage.example.Examp
I get a report:
Error: Could not find or load main class com.mypackage.example.Pokus
Caused by: java.lang.ClassNotFoundException: com.mypackage.example.Pokus
Second attempt:
java -cp "C:\Users\pfort\Desktop\java\jars\json-20200518.jar" "C:\Users\pfort\Desktop\java\com\mypackage\example\Pokus"
But the same error message comes back to me.
Where am I going wrong? Is it the wrong structure? I don't get it, the compilation is successful but the run does not work.
The compiled Examp.class file isn't part of json-20200518.jar, so you'll need to add the directory containing it to the command line. Assuming it's the current directory (.):
java -cp "C:\Users\pfort\Desktop\java\jars\json-20200518.jar;." com.mypackage.example.Examp

On using a batch file in cmd Could not find or load main class for a program

The error image
Error: Could not find or load main class JDBCExample1.java
Caused by: java.lang.ClassNotFoundException:JDBCExample1.java
Batch file used:
set path=C:\Program Files\Java\jdk-10.0.2\bin;
set classpath=C:\oraclexe\app\oracle\product\11.2.0\server\jdbc\lib\ojdbc14.jar;.
javac JDBCExample1.java
java JDBCExample1.java
pause
The PATH and the CLASSPATH have been set correctly in Environment variables too.
Also the directory used is correct.
Try giving
“java JDBCExample1”
Instead of
“java JDBCExample1.java”
In the last line. One does not use .java while running the program

How do I add `odex` files to the classpath for dalvikvm?

This question is a follow-up to my earlier question.
Here is the same example from that question.
import android.os.SystemClock;
/**
* Command that sends key events to the device, either by their keycode, or by
* desired character output.
*/
public class MWE {
public static void main(String[] args) {
System.out.println(SystemClock.uptimeMillis());
}
}
After I looked around in my /system/framework directory, I discovered that the class android.os.SystemClock is defined in framework.odex on my phone. I naturally tried the following two command to try to access it.
/system/bin/dalvikvm -Xbootclasspath:/system/framework/core.jar -classpath /system/framework/framework.odex:/data/local/tmp/MWE.jar MWE
/system/bin/dalvikvm -Xbootclasspath:/system/framework/core.jar:/system/framework/framework.odex -classpath /data/local/tmp/MWE.jar MWE
However, both of them resulted in the same error message about not being able to find the class definition.
How do I add such odex files to the classpath for dalvikvm?
Have you tried just:
/system/bin/dalvikvm -classpath /data/local/tmp/MWE.jar MWE
As far as I know, in this case it will pull in the boot classpath from the BOOTCLASSPATH environment variable, which should already contain core.jar and framework.jar.
However, I suspect that will actually result in an UnsatisfiedLinkError exception, because the JNI library that implements some of the native methods in SystemClock won't be loaded.
In that case, there's a handy utility class available that should load the native libraries.
dalvikvm -classpath /data/local/tmp/MWE.jar com.android.internal.util.WithFramework MWE

Main Class Not Found

I am to connect to a MySQL database from a Java application to store images taken from a scanned form. When I run the program I get a Could not find or load main class error.
I have tried setting the classpath but still get the same error: Could not find or load main class. By the way, another java file I created in the directory compiles. When I try to set the classpath I also get the same error. Here is the (EDITED)code:
java -cp "C:\Documents and Settings\xxx\Desktop\ttt_manual_yyy\mysql\mysql-connector-java-5.1.6-bin" ImageUploader C:\Documents and Settings\yyy\Desktop\IMAGES localhost uuu nnn
Where localhost is database address,uuu is username,nnn is password
You're not even specifying a class, and you need to put your classpath in quotes as it's got spaces. So it should be something like:
java -classpath "C:\Documents and Settings\...\ImageUpload" foo.ImageUploader
where foo.ImageUploader is the fully-qualified name of the class containing the main method.

Understanding JARs and Packages in Java

I'm trying to understand how jars and packages work in Java. So to do this, I created a simple test JAR and am trying to use a class contained in that jar. Simple enough, but it is giving me errors like "class not found". Here's the setup:
1) I have a file called MyHelloWorld.java, which will be packaged in a JAR:
package com.mytest;
public class MyHelloWorld {
public String getHello() {
return "Hello";
}
public static void main(String[] args) {
System.out.println("Hello world!");
}
}
2) I have another file called 'HelloHello.java' which uses the function getHello() in com.mytest.MyHelloWorld
import com.mytest.*;
public class HelloHello {
public static void main (String[] args) {
MyHelloWorld hello = new MyHelloWorld();
System.out.println(hello.getHello());
}
}
3) To package the MyHelloWorld class inside a JAR, I created the folders com/mytest in the current directory, and moved MyHelloWorld.java to that folder
4) I compiled MyHelloWorld.java in that folder using javac MyHelloWorld.java
5) I ran jar -cf myhello.jar ./com/mytest/*.class from the root folder to create the JAR file (as described in http://www.javacoffeebreak.com/faq/faq0028.html)
6) I copied HelloHello.java and myhello.jar to a new folder with nothing else in it, to test this setup
7) javac -cp ./*.jar HelloHello.java [succeeds]
8) java -cp ./*.jar HelloHello [FAILS] (I also tried just `java HelloWorld', which failed too, with a different error message)
This last statement fails with the message:
$java -cp ./*.jar HelloHello
Exception in thread "main" java.lang.NoClassDefFoundError: HelloHello
Caused by: java.lang.ClassNotFoundException: HelloHello
at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:315)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:330)
at java.lang.ClassLoader.loadClass(ClassLoader.java:250)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:398)
Any idea why it's failing? Any insights you can provide on why it works this way, and how package names are defined inside a JAR etc. would also be appreciated!
I believe it is looking in the jar for your HelloHello class. You probably need the current folder on the classpath too.
java -cp .:myhello.jar HelloHello
You should use:
java -cp .:./* HelloHello
java and javac treat -cp argument a bit differently. With java the * in cp will automatically load all the jars it finds in the given location.
Also, the colon : is the separator between different classpath elements.
Make sure if HelloHello.class is in appropriate directories structure (com/mytest) than change your 8th step:
8) java com.mytest.HelloHello //or java -cp .;*.jar com.mytest.HelloHello
well, java HelloHello works too

Categories