Hot questions for Using Ubuntu in classpath

Question:

Do the second command add the current path as another path for searching?

javac -cp /home/tim/program_files/programming/java/junit-4.11.jar MyTest.java

javac -cp .:/home/tim/program_files/programming/java/junit-4.11.jar MyTest.java

Is it the correct way to separate multiple paths, by a colon?

Isn't the current path always in ClassPath by default, and thus no need to explicitly specify?

Thanks.


Answer:

From Oracle's page on setting the class path:

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, you must include "." in the new settings.

Use ; for Windows and : for Unix-like operating systems as a separator for multiple paths.

Question:

I'm trying to import the class from jar files. I want to place all of the jar files in the directory:

/home/somebody/lib/java/

Compiling with the command given below says:

package com.a.A does not exits**. java C.java

I set the following in profile.

CLASSPATH=/home/somebody/lib/java

then exported it. Added to PATH as well

C.java
import com.a.A;
import com.a.AA;
import com.b.B;
import com.b.BB;

class B {
    public static void main() {
        new A();
        new AA();
        new B();
        new BB();
    }
}

the classfiles are in the following jar_file a.jar and b.jar.


Answer:

You should add your jar files to your CLASSPATH instead:

export CLASSPATH=/home/somebody/lib/java/a.jar:/home/somebody/lib/java/b.jar

The classpath supports directories (with *.class files) or individual jar files. You can also add wildcards (or use java -classpath option) but I will leave that off for now to don't further complicate things ;)

By the way, you don't need to change your PATH environment variable at all. The CLASSPATH is more than enough.

Question:

It is said that Java is portable regardless of the OS. But here is my doubt: why the java prompts work perfectly in windows but, in my case, fails to execute the java prompt.

My folder structure is as follows:

c:
--|temp
-----|JavaSDKv2
--------|com
-----------|impinj
--------------|octanesdk
-----------------|samples
--------------------|(.java files)
--------------------|(.class files)
--------|OctaneSDKJava-1.20.2.240-jar-with-dependencies.jar

Example: (In windows 8, in the cmd)

    C:\temp>cd JavaSDKv2
    C:\temp\JavaSDKv2>javac com\impinj\octanesdk\samples\*.java    -classpath      OctaneSDK
    Java-1.20.2.240-jar-with-dependencies.jar

    C:\temp\JavaSDKv2>

(therefore no error and each java file with a class defined has now a .class file for each class defined in the .java files )





C:\temp\JavaSDKv2>java -Dhostname=192.168.2.203 -classpath OctaneSDKJava-1.20.2. 240-jar-with-dependencies.jar;. com.impinj.octanesdk.samples.RfDoppler 
Connecting to 192.168.2.203 
log4j:WARN No appenders could be found for logger (org.apache.mina.filter.execut or.ExecutorFilter). 
log4j:WARN Please initialize the log4j system properly.

Press Enter to exit.  
EPC: 0100 0000 0000 9950 0000 0000 Reader_ip:    192.168.2.203 doppler: .5625

EPC: 0100 0000 0000 9950 0000 0000 Reader_ip: 192.168.2.203 doppler: 3.0

EPC: 0100 0000 0000 9950 0000 0000 Reader_ip: 192.168.2.203 doppler: -6.6875 

(and this goes on without any problem)

Then I use Ubuntu (in virtual box). I check the connection.

    lloret@lloret-VirtualBox:~/temp/JavaSDKv2$ ping 192.168.2.203
    PING 192.168.2.203 (192.168.2.203) 56(84) bytes of data.
    64 bytes from 192.168.2.203: icmp_seq=1 ttl=63 time=2.56 ms
    64 bytes from 192.168.2.203: icmp_seq=2 ttl=63 time=1.64 ms
    64 bytes from 192.168.2.203: icmp_seq=3 ttl=63 time=1.56 ms
    64 bytes from 192.168.2.203: icmp_seq=4 ttl=63 time=1.34 ms
    64 bytes from 192.168.2.203: icmp_seq=5 ttl=63 time=1.60 ms

And I check the java version (java -version)

lloret@lloret-VirtualBox:~/temp/JavaSDKv2$ java -version
java version "1.7.0_79"
OpenJDK Runtime Environment (IcedTea 2.5.6) (7u79-2.5.6-0ubuntu1.14.04.1)
OpenJDK Client VM (build 24.79-b02, mixed mode, sharing)

 lloret@lloret-VirtualBox:~/temp/JavaSDKv2$ javac com/impinj/octanesdk/samples/*.java -classpath OctaneSDKJava-1.20.2.240-jar-with-dependencies.jar
lloret@lloret-VirtualBox:~/temp/JavaSDKv2$

(and this creates the .class files)


lloret@lloret-VirtualBox:~/temp/JavaSDKv2$ java -Dhostname=192.168.2.203 -classpath OctaneSDKJava-1.20.2.240-jar-with-dependencies.jar com.impinj.octanesdk.samples.RfDoppler
Error: Could not find or load main class com.impinj.octanesdk.samples.RfDoppler
lloret@lloret-VirtualBox:~/temp/JavaSDKv2$

Strangely java doesn't find any kind of .class file. My guess is that is something related to the package. Of course if someone writes a helloworld.java code and the executes the helloworld.class, you see "Hello, World!"


Answer:

On ubuntu your default classpath does not contain the current directory, so try it this way:

lloret@lloret-VirtualBox:~/temp/JavaSDKv2$ java -Dhostname=192.168.2.203 -classpath OctaneSDKJava-1.20.2.240-jar-with-dependencies.jar:. com.impinj.octanesdk.samples.RfDoppler

Question:

When I run the following in Windows it works correctly:

java -cp bin\;lib/myJar.jar com.example.MyClass

But I cannot get it to work in Ubuntu (it says Could not find or load main class). I read a couple of posts in SO, and the problem I think is in the way the class path needs to be set, but I cannot figure out how I can modify bin\;lib/myJar.jar to run correctly on ubuntu.


Answer:

You are executing your cmd in UNIX system , you should follow convention of unix :

don't use \ as directory separator, use / instead. don't use ; as classpath separator , use : instead.

so you have to move to the directory where your myJar.jar is located, if the main class that you want to execute is supposed to be in the jar ,then check that it's really there using the command :

jar tf jar-file

that will print the content of your jar.if you have a folder bin in the same folder you can add it to the classpath

then use

java -cp "bin/:lib/myJar.jar" com.example.MyClass

Question:

I have the following Java application structure:

  • Project name: GoodManagement
  • Main package: goodmanagement
  • Main class path: goodmanagement.GoodManagementLauncher

I build the application with NetBeans on Windows. When launching on Windows the applications starts perfectly. The same applies to launching the jar on Windows.

When copying the jar to Ubuntu Mate on my Raspberry Pi 2 (Oracle JDK 8 for ARM32 v7) I cannot launch the jar with java -jar GoodManagement.jar. It tells me the main class could not be found or loaded.

I tried the following and was not successful:

  1. Setting the CLASSPATH variable ($CLASSPATH) with export CLASSPATH=., CLASSPATH=.:[PATH TO CLASSES FOLDER].
  2. Setting the classpath with -cp or -classpath like above.
  3. Use the absolute path to the jar file as -classpath.
  4. Use the path to the classes folder (like in 1.) and then use the fully qualified class name (goodmanagement.GoodManagementLauncher)

So what can I do to launch my application on Ubuntu?

EDIT1: The output for building the application:

ant -f D:\\Documents\\Fachschaft\\GoodManagement\\GoodManagement jfx-rebuild
init:
deps-clean:
Updating property file: D:\Documents\Fachschaft\GoodManagement\GoodManagement\build\built-clean.properties
Deleting directory D:\Documents\Fachschaft\GoodManagement\GoodManagement\build
clean:
init:
deps-jar:
Created dir: D:\Documents\Fachschaft\GoodManagement\GoodManagement\build
Updating property file: D:\Documents\Fachschaft\GoodManagement\GoodManagement\build\built-jar.properties
Created dir: D:\Documents\Fachschaft\GoodManagement\GoodManagement\build\classes
Created dir: D:\Documents\Fachschaft\GoodManagement\GoodManagement\build\empty
Created dir: D:\Documents\Fachschaft\GoodManagement\GoodManagement\build\generated-sources\ap-source-output
Compiling 39 source files to D:\Documents\Fachschaft\GoodManagement\GoodManagement\build\classes
Note: Some input files use unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
Copying 12 files to D:\Documents\Fachschaft\GoodManagement\GoodManagement\build\classes
compile:
Created dir: D:\Documents\Fachschaft\GoodManagement\GoodManagement\dist
Detected JavaFX Ant API version 1.3
Launching <fx:jar> task from C:\Program Files\Java\jdk1.8.0_51\jre\..\lib\ant-javafx.jar
Warning: From JDK7u25 the Codebase manifest attribute should be used to restrict JAR repurposing.
Please set manifest.custom.codebase property to override the current default non-secure value '*'.
Launching <fx:deploy> task from C:\Program Files\Java\jdk1.8.0_51\jre\..\lib\ant-javafx.jar
jfx-deployment-script:
jfx-deployment:
jar:
jfx-rebuild:
BUILD SUCCESSFUL (total time: 6 seconds)

And now the manifest file:

Manifest-Version: 1.0
Implementation-Title: GoodManagement
Implementation-Version: 1.0
Permissions: sandbox
Codebase: *
JavaFX-Version: 8.0
Class-Path: 
Created-By: JavaFX Packager
Implementation-Vendor: Daniel
Main-Class: goodmanagement.GoodManagementLauncher

Answer:

The problem was that I was trying to run a JavaFX application in a VM that has no JavaFX support (arm32 jdk1.8.60). When trying to compile the source code I discovered that the compiler was unable to find the JavaFX classes.

Question:

I'm trying to use the algs4 libraries for the Sedgewick's book Algorithms 4 and the things aren't working.

I think I'm following correctly the guide from the website http://algs4.cs.princeton.edu/code/.

I saved the files stdlib.jar and algs4.jar in the directory ~/algs4 and append the line "export CLASSPATH=$CLASSPATH:~/algs4/stdlib.jar:~/algs4/algs4.jar" in the ~/.profile file.

The things should work, right? Where am I going wrong?


Answer:

Run

. ~/.profile 

to refresh your profile?

EDIT

Normally running a command in Linux will create a new subshell and execute that. This means that any variables set in the command will be lost when the subshell exits. To make the variables persist, you can either export them or run the command in the current shell by using the source or . command.

EDIT

To know what your profile file is, you need to know which shell you are running. Use the ps command and see what the CMD is:

$ ps
  PID TTY          TIME CMD
17692 pts/5    00:00:00 zsh
23736 pts/5    00:00:00 ps

This shows that I am running the zsh shell. If you just see sh, you can't immediately tell, so you should try running

sh --version