Ronaldo Bahia
Ronaldo Bahia

Reputation: 576

Python call Java causes Error: Could not find or load main class

I am having issues to use the java program https://github.com/antonydeepak/ResumeParser/ in Django project in a localserver in MAC OS.

I have installed ResumeParser in the Django Project like:

-- Django Project -- app1 -- app2 -- ResumerParser

Here is my code but it says "Could not find or load main class".

if form.is_valid():
    f = form.save(commit=False)

    resume = form.cleaned_data['resume']

    cmd = ['java', '-cp', 'bin/:../GATEFiles/lib/:../GATEFiles/bin/gate.jar:lib/*', 'code4goal.antony.resumeparser.ResumeParserProgram %s textOutput.json' % resume]

    subprocess.Popen(cmd)

Any clues of how to solve this? I've tried every post related to this theme in StackOverflow no success.

Thanks in advance

Upvotes: 1

Views: 1838

Answers (2)

Ronaldo Bahia
Ronaldo Bahia

Reputation: 576

After two weeks I got it working. All files must be copied to the ResumeParser/ResumeTransducer dir.

Also, there's a need to inform the current dir for file parsing.

Here is the implementation:

# first save the file
if form.is_valid():
    f = form.save(commit=False)

    resume = form.cleaned_data['resume']
    f.resume = resume
    f.save()

    # copy file to the CV parser dir so java can parse the file
    cf = "." + f.resume.url
    shutil.copy2(cf, 'ResumeParser/ResumeTransducer')

    # get file to convert
    fl_name = str(f.resume).split('/')[-1]

    # get file name to make json output
    base_name = os.path.splitext(fl_name)[0]

    cmd = "java -cp 'bin/*:../GATEFiles/lib/*:../GATEFiles/bin/gate.jar:lib/*' code4goal.antony.resumeparser.ResumeParserProgram %s %s.json" % (fl_name, base_name)

    # get the current working dir       
    os.chdir("ResumeParser/ResumeTransducer")

    # call java
    subprocess.Popen(cmd, shell=True)

Thanks Jean-François Fabre!

Upvotes: 0

Jean-François Fabre
Jean-François Fabre

Reputation: 140297

You're mixing up well-delimiter parameters with parameters grouped with spaces.

cmd = ['java', '-cp', 'bin/:../GATEFiles/lib/:../GATEFiles/bin/gate.jar:lib/*', 'code4goal.antony.resumeparser.ResumeParserProgram %s textOutput.json' % resume]

Your last parameter is seen as a single parameter and is protected by spaces by subprocess:

"code4goal.antony.resumeparser.ResumeParserProgram resume_value textOutput.json"

=> The whole "class<space>param1<space>param2" is seen as your class: no wonder why it's not found.

Split all your parameters and it will work, subprocess won't group your parameters, no quoting (note the forced conversion of the resume object to str):

cmd = ['java', '-cp', 'bin/:../GATEFiles/lib/:../GATEFiles/bin/gate.jar:lib/*', 'code4goal.antony.resumeparser.ResumeParserProgram', str(resume),'textOutput.json']

Upvotes: 1

Related Questions