skip to Main Content

i am running java source code on windows. Now I want it to run on Ubuntu but it doesn’t work

Below is the bat file on windows:

@echo off
color 1f
title UC
C:*****Javajdk1.8.0_121binjava -Xmx512m -Xms256m -Xss256k -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=./oom.hprof -XX:+UnlockExperimentalVMOptions -XX:+UseG1GC -XX:MaxGCPauseMillis=50 -XX:GCPauseIntervalMillis=200 -XX:SurvivorRatio=6 -Xloggc:logs/gc.log -classpath "./;./lib/*;./config" com.playmore.http.jetty.SpringContextLoader
pause

And here I switched to ubuntu .sh:

#!/bin/bash
echo -e "e[34m"
echo -e "e[1mUCe[0m"
java -Xmx512m -Xms256m -Xss256k -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=./oom.hprof -XX:+UnlockExperimentalVMOptions -XX:+UseG1GC -XX:MaxGCPauseMillis=50 -XX:GCPauseIntervalMillis=200 -XX:SurvivorRatio=6 -Xloggc:logs/gc.log -classpath "./:./lib/*:./config" com.playmore.http.jetty.SpringContextLoader
read -p "Press Enter to continue..."

this is the error shown on ubuntu:

Error: Could not find or load main class com.playmore.http.jetty.SpringContextLoader
Caused by: java.lang.ClassNotFoundException: com.playmore.http.jetty.SpringContextLoader
Press Enter to continue...

I don’t know if it’s because -classpath "./:./lib/*:./config" was not found or not, I hope it can run normally on ubuntu like windows

2

Answers


  1. you may want to check if the class com.playmore.http.jetty.SpringContextLoader is present in the specified classpath on your Ubuntu system. Additionally, verify that the directory structure and file locations match

    Login or Signup to reply.
  2. Your error is in your quote usage. In most linux shells including bash, "foo*" will apply that *. A key difference is this:

    • In windows, it is the application that applies star-globbing (so when you run someApp *.txt, windows’ cmd.exe first breaks that apart into someApp, *.txt, will then use $PATH to figure out that you’re referring to C:Program FilesfoobarsomeApp.exe and will launch it, passing *.txt as args array. The someApp app then receives this and calls some windows API stuff to procure a list of files that match the glob *.txt and operates on each one.
    • In linux, bash takes that line and breaks it into bits similar to windows, and uses path similar to windows to figure out which app to launch. However, it is then bash itself that applies *.txt -> list of files conversion, and bash will end up giving the someApp app as ‘these are your command line args’ the list: [file1.txt, file2.txt, file3.txt].

    (NB: This explains why using ProcessBuilder or Runtime.exec to pass *.txt to a linux process does not work: It’s bash that applies globs, and ProcessBuilder is not bash).

    bash still does this even if you use double quotes: The double quotes ensure that bash does not consider a space as a signal to go: End of one parameter, on to the next parameter. If you want bash to also cease attempting to ‘interpret’ the command line (which isn’t just translating * to stuff, also $FOO and similar variable replacements), you need single quotes.

    Because the classpath in java are colon separated (Semicolon on windows) and not space-separated, bash mucks this up – it sees that * and breaks it into separate arguments, which is not what java can handle.

    Hence, java, uniquely, does its own * expansion. This is why it has to be just *, not *.jar, or com.foo.*.jar – bash can handle those but java.exe‘s own system can only deal with some/dir/*.

    And, because java does it, you must tell bash not to, and you do that with single quotes:

    java -Xmx512m -Xms256m -Xss256k -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=./oom.hprof -XX:+UnlockExperimentalVMOptions -XX:+UseG1GC -XX:MaxGCPauseMillis=50 -XX:GCPauseIntervalMillis=200 -XX:SurvivorRatio=6 -Xloggc:logs/gc.log -classpath './:./lib/*:./config' com.playmore.http.jetty.SpringContextLoader
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search