Forum Stats

  • 3,837,410 Users
  • 2,262,256 Discussions
  • 7,900,273 Comments

Discussions

Java Web Start command is not working (java -cp class_path class_name)

vitaxa
vitaxa Member Posts: 2
edited Apr 19, 2018 11:55AM in Java Programming

I have the app, which i launch with "Java Web Start". This app is used for launching other java apps. The issue is that with the command "java -cp class_path class_name" java app cannot be launched, an exception "Could not find or load main class class_name" appears.

However, Class.forName(class_name) doesn't throw exception: "ClassNotFoundException" and i can run static method from another jar(but it's in current JVM).

Without using "Java Web Start" everything is alright.

Answers

  • Unknown
    edited Apr 18, 2018 11:07AM
    The issue is that with the command "java -cp class_path class_name" java app cannot be launched, an exception "Could not find or load main class class_name" appears.

    That exception pretty much means what it says: the class can't be found on the 'unknown to us' classpath you have set.

    Since you haven't SHOWN US any info about what that classpath is or what it contains the only thing we can do is refer you to the docs:

    https://docs.oracle.com/javase/8/docs/technotes/tools/windows/classpath.html

    classpath1:classpath2    Class paths to the JAR, zip or class files. Each class path should end with a file name or directory depending    on what you are setting the class path to, as follows:        For a JAR or zip file that contains class files, the class path ends with the name of the zip or JAR file.        For class files in an unnamed package, the class path ends with the directory that contains the class files.        For class files in a named package, the class path ends with the directory that contains the root package, which        is the first package in the full package name.    Multiple path entries are separated by semicolons with no spaces around the equals sign (=) in Windows and colons    in Oracle Solaris.    The default class path is the current directory. Setting the CLASSPATH variable or using the -classpath command-line    option overrides that default, so if you want to include the current directory in the search path, then you must    include a dot (.) in the new settings.    Class path entries that are neither directories nor archives (.zip or JAR files) nor the asterisk (*) wildcard character are ignored.

    The above are just PART of the rules.

    Common mistakes people make are ONLY including the path instead of the actual jar names and not using an * when they need to.

  • vitaxa
    vitaxa Member Posts: 2
    edited Apr 19, 2018 4:15AM

    When I launch the common app, I use the command: java -cp *; com.vitaxa.voidbox.MainApp. In this way I form the classpath, collecting everything from the current directory.

    Then I can launch other apps, using the command: java -cp current_classpath class_name. So everything works well, but without using Java WebStart.

    There is the difference. For getting the classPath I use the next method:

    private static String getClasspath() {

            final StringBuilder sb = new StringBuilder();

            ClassLoader classLoader = TaskRunner.class.getClassLoader();

            if (classLoader == null) {

                classLoader = ClassLoader.getSystemClassLoader();

            }

            final URL[] urls = ((URLClassLoader) classLoader).getURLs();

            for (URL url : urls) {

                sb.append(url.getFile()).append(";");

            }

            return sb.toString();

        }

    In case of running with an ordinary app, the classpath is: /D:/WorkDir/Applications/FirstApp.jar;/D:/WorkDir/Applications/SecondApp.jar;/D:/WorkDir/Applications/ThirdApp.jar;

    and running with using Java WebStart the classpath is: /Applications/FirstApp.jar;/Applications/SecondApp.jar;/Applications/ThirdApp.jar.

    P.S. I tried using System.getProperty("java.class.path"), it hadn't helped.

  • Unknown
    edited Apr 19, 2018 11:55AM
    When I launch the common app,

    As far as Java is concerned there is NO SUCH THING as a 'common app'.

    I use the command: java -cp *; com.vitaxa.voidbox.MainApp. In this way I form the classpath, collecting everything from the current directory.

    Correct - that 'current directory' will be whatever directory is current for the shell being used to launch java.

    Then I can launch other apps, using the command: java -cp current_classpath class_name.

    And launching those 'other apps' has NOTHING to do with the first app you launched since they get their own shell and their own JVM.

    And their environment will depend, in part, of the actual value of 'current_classpath' which you did NOT post. But whatever the contents of that are they will control where the NEW JVM will look for jars and classes.

    Print out the value of that 'current_classpath' so you can see what it is.

    There is the difference. For getting the classPath I use the next method:

    Print it out so you can SEE IT - or, better, write it to a log file.

    In case of running with an ordinary app, the classpath is: 
    /D:/WorkDir/Applications/FirstApp.jar;/D:/WorkDir/Applications/SecondApp.jar;/D:/WorkDir/Applications/ThirdApp.jar;

    Print it out or write it to a log file so you can SEE IT!

    and running with using Java WebStart the classpath is: /Applications/FirstApp.jar;/Applications/SecondApp.jar;/Applications/ThirdApp.jar.

    And there is your problem.

    See that first character? The 'slash'? That means 'start at the root'.

    But the root does NOT start with 'Applications' does it? It starts with 'D:/WorkDir/'.

    All you need to do is compare the two paths to see they are totally different.

    They need to be the same in order to work the same.

This discussion has been closed.