j2objc: command not found - java

I have to convert java code to objective-c code.
Here I have to write the following code and run on my terminal:
$ cat Hello.java
public class Hello {
public static void main(String[] args) {
System.out.println("hello, world");
}
}
$ j2objc Hello.java
translating Hello.java
Translated 1 file: 0 errors, 0 warnings
To compile the translated file:
$ j2objcc -c Hello.m
$ j2objcc -o hello Hello.o
Here when I run $ j2objcc -o hello Hello.o on my terminal, I get the following error.
$ j2objcc -o hello Hello.o
Undefined symbols:
"_objc_autoreleasePoolPop", referenced from:
-[JavaLangThread run] in libjre_emul.a(Thread.o)
"_objc_autoreleasePoolPush", referenced from:
-[JavaLangThread run] in libjre_emul.a(Thread.o)
ld: symbol(s) not found
clang: error: linker command failed with exit code 1 (use -v to see invocation)
How can I resolve this error?

You need to add the path to where you unzipped the j2objc distribution bundle. For example:
$ cd $HOME
$ unzip ~/Downloads/j2objc-*.zip
$ export PATH=${PATH}:${HOME}/j2objc
Note: the export command above needs to be added to your ${HOME}/.bashrc file so it's set when you log out and back in.

You comment you are using XCode 3.2.6. Currently, J2ObjC seems to require XCode 4+.
Reference: https://code.google.com/p/j2objc/

Related

Invalid flag error when using bash to compile java

I am trying to write a simple script to compile my java code.
When I put
javac Main.java
in my compile.sh and do
bash compile.sh
I get error: invalid flag: Main.java
However, if I just simply use the command javac Main.java , everything works fine.
I am using a Ubuntu VirtualBox on Win10.
How can I get my script working?
Here's the problem you're seeing:
$ cat compile.sh
javac Main.java
$ bash compile.sh
javac: invalid flag: Main.java
Usage: javac <options> <source files>
use -help for a list of possible options
This happens because the script uses DOS style line endings:
$ cat -v compile.sh
javac Main.java^M
You can fix it by setting your editor to save it with Unix line terminators, or with e.g. tr:
$ tr -d '\r' < compile.sh > fixed.sh
(no output)
$ cat -v fixed.sh
javac Main.java
It now works as expected:
$ bash fixed.sh
(no output)

Why cannot run compiled JavaFX 11 class?

Following instructions at https://openjfx.io/openjfx-docs/#install-javafx, I compiled the sample HelloFX.java via:
javac --module-path $PATH_TO_FX --add-modules=javafx.controls /Users/me/Documents/java/HelloFX.java
But now if I attempt to run that...
java --module-path $PATH_TO_FX --add-modules=javafx.controls /Users/me/Documents/java/HelloFX
... I get error:
Error: Could not find or load main class .Users.me.Documents.java.HelloFX
Caused by: java.lang.ClassNotFoundException: /Users/me/Documents/java/HelloFX
Yet the file reported as not found is there:
ls -l /Users/me/Documents/java/HelloFX.class
-rwxr--r-- 1 me staff 1336 Oct 30 16:01 /Users/murray/Documents/java/HelloFX.class
(I had already changed permissions to add u+x in case that was the issue, but apparently that was not the trouble.
What's wrong?
(Yes, $PATH_TO_FX does point to javafx-sdk-11/lib.)
This question was already answered in the openjfx-dev mailing list:
The "java" command expects a fully-qualified class name, not a file path as
its argument
For completion:
The javac command deals with filenames, which means you can compile a java file from any location:
javac [ options ] [ sourcefiles ]
However the java command deals with classes:
java [options] mainclass [args...]
where mainclass specifies the name of the class to be launched, not the filename or location.
Providing you have Java 11 installed (and JAVA_HOME is set at it), the JavaFX 11 SDK downloaded, and just following the getting started guide:
Download the HelloFX class to any location, i.e /Users/<user>/Downloads.
Open a terminal and cd to that location:
cd /Users/<user>/Downloads
Set the JavaFX path:
export PATH_TO_FX=/path/to/javafx-sdk-11/lib
Compile the class:
javac --module-path $PATH_TO_FX --add-modules=javafx.controls HelloFX.java
Check that HelloFX.class is created at the same folder level.
Run the class:
java --module-path $PATH_TO_FX --add-modules=javafx.controls HelloFX
It should run just fine.
Now, if you try to run the above command from a different location it won't work, because the HelloFX class is not available in the classpath.
So if you want to run this class from a different location you'll need to specify this classpath:
javac --module-path $PATH_TO_FX --add-modules=javafx.controls \
/Users/<user>/Downloads/HelloFX.java
java --module-path $PATH_TO_FX --add-modules=javafx.controls \
--class-path /Users/<user>/Downloads HelloFX

How to build and run a java instance in Docker

I am trying to learn my way around Docker. I want to build a container/image for my java application. The Docker file looks like this:
FROM openjdk:7
COPY . C:/Users/name/Documents/NetBeansProjects/project1/src/mainpckg
WORKDIR C:/Users/name/Documents/NetBeansProjects/project1/src/mainpckg
RUN javac Main.java
CMD java Main
And i am calling it like this:
docker build -t my-java-app .
But it gives the following error:
$ docker build -t my-java-app .
Sending build context to Docker daemon 3.072 kB
Step 1 : FROM openjdk:7
---> 4a42f05dc422
Step 2 : COPY . C:/Users/name/Documents/NetBeansProjects/project1
/src/mainpckg
---> Using cache
---> 301de702fad9
Step 3 : WORKDIR C:/Users/name/Documents/NetBeansProjects/project1
s/src/mainpckg
---> Using cache
---> 1868e40b329e
Step 4 : RUN javac Main.java
---> Running in 66d7d769f425
javac: file not found: Main.java
Usage: javac <options> <source files>
use -help for a list of possible options
The command '/bin/sh -c javac Main.java' returned a non-zero code:
2
I also tried the solution given here:
docker run --rm -v /mypathhere/mycode java:7 sh -c "cd mycode; javac mycode.java"
but without any results, i still got this error:
javac: file not found: Main.java
Usage: javac <options> <source files>
use -help for a list of possible options
There is something fishy about C:/. Try following, it must work
FROM java:8
# add the container directory from the host
RUN mkdir /opt/mainpckg
# copy the app to container directory
ADD . /opt/mainpckg
WORKDIR /opt/mainpckg
RUN javac Main.java
CMD java Main
I don't know why you run with -v /mypathhere/mycode, as it creates a data volume which would overlay (temporarily overwrite) anything that was /mypathhere/mycode from the Dockerfile.
So try running without the -v part, in case /mypathhere/mycode is C:/Users/name/Documents/NetBeansProjects/project1 (assuming you are using Docker on Windows with Windows containers)

Error: Could not find or load main class Swagger Codegen

I left an open issue for here
I´m trying to create a custom codegen, I managed to make it work by putting the files inside of the codegen project but I want it to work like this: https://github.com/swagger-api/swagger-codegen#making-your-own-codegen-modules
I haven't modified the autogenerated project at all but I keep getting:
Error: Could not find or load main class io.swagger.codegen.SwaggerCodegen
This is the command line:
java -cp output/myLibrary/target/myCustomCodegen-swagger-codegen-1.0.0.jar:swagger-codegen-cli-2.1.6.jar io.swagger.codegen.SwaggerCodegen generate -i https://watson-api-explorer.mybluemix.net/listings/conversation-v1-experimental.json -l com.my.company.codegen.Mycustomcodegengenerator -o outputlocation
I got the jar from here https://mvnrepository.com/artifact/io.swagger/swagger-codegen-project/2.1.6
This is what I'm doing:
Run java -jar swagger-codegen-cli-2.1.6.jar meta \ -o output/myLibrary -n myClientCodegen -p com.my.company.codegen to create costume codegen
Run mvn package in output/myLibrary
Run java -cp output/myLibrary/target/myCustomCodegen-swagger-codegen-1.0.0.jar:swagger-codegen-cli-2.1.6.jar io.swagger.codegen.SwaggerCodegen generate -i https://watson-api-explorer.mybluemix.net/listings/conversation-v1-experimental.json -l com.my.company.codegen.Mycustomcodegengenerator -o outputlocation in the folder that contains both swagger-codege-cli-2.1.6.jar and the output folder
It does find the class if I remove the first part, but doesn´t find the new language:
java -cp swagger-codegen-cli-2.1.6.jar io.swagger.codegen.SwaggerCodegen generate -i https://watson-api-explorer.mybluemix.net/listings/conversation-v1-experimental.json -l com.my.company.codegen.Mycustomcodegengenerator -o outputlocation
I have looked at the answers for "Error: Could not find or load main class" problems but haven't manage to fix it.
Here is a link to the jar
For Windows, change the colon (:) to a semicolon (;) - between the jars in the class path. So instead of
java -cp output/myLibrary/target/myCustomCodegen-swagger-codegen-1.0.0.jar:swagger-codegen-cli-2.1.6.jar io.swagger.codegen.SwaggerCodegen generate -i https://watson-api-explorer.mybluemix.net/listings/conversation-v1-experimental.json -l com.my.company.codegen.Mycustomcodegengenerator -o outputlocation
It should be
java -cp output/myLibrary/target/myCustomCodegen-swagger-codegen-1.0.0.jar;swagger-codegen-cli-2.1.6.jar io.swagger.codegen.SwaggerCodegen generate -i https://watson-api-explorer.mybluemix.net/listings/conversation-v1-experimental.json -l com.my.company.codegen.Mycustomcodegengenerator -o outputlocation
Multiple class paths need to be separated by a semicolon. http://docs.oracle.com/javase/7/docs/technotes/tools/windows/classpath.html
The problem is that you don't specify that correct path of swagger-codegen-2.1.6.jar in your call. Thats why it can't find the main-class.
If you are inside the root project swagger-codegen you should specify it like this: modules/swagger-codegen-cli/target/swagger-codegen-cli.jar
~$ cd ~/git/swagger-codegen # go into your root project
~/git/swagger-codegen$ # ... do the steps you described
~/git/swagger-codegen$ java -cp \
output/myLibrary/target/myClientCodegen-swagger-codegen-1.0.0.jar:modules/swagger-codegen-cli/target/swagger-codegen-cli.jar \
io.swagger.codegen.SwaggerCodegen \
generate -i https://watson-api-explorer.mybluemix.net/listings/conversation-v1-experimental.json \
-l com.my.company.codegen.Mycustomcodegengenerator \
-o outputlocation
Or as a one-liner:
~/git/swagger-codegen$ java -cp output/myLibrary/target/myClientCodegen-swagger-codegen-1.0.0.jar:modules/swagger-codegen-cli/target/swagger-codegen-cli.jar io.swagger.codegen.SwaggerCodegen generate -i https://watson-api-explorer.mybluemix.net/listings/conversation-v1-experimental.json -l com.my.company.codegen.Mycustomcodegengenerator -o outputlocation
Update 1
I'm pretty sure when you construct the classpath with -cp you have a mistake with the swagger-codegen-cli-2.1.6.jar. Please test the following.
Copy both (myClientCodegen-swagger-codegen-1.0.0.jar and swagger-codegen-cli-2.1.6.jar) jars into the same folder. Then go into this folder and try the following:
javap -cp myCustomCodegen-swagger-codegen-1.0.0.jar:swagger-codegen-cli-2.1.6.jar io.swagger.codegen.SwaggerCodegen
javap checks whether the main class io.swagger.codegen.SwaggerCodegen is available. On my machine it prints this:
Compiled from "SwaggerCodegen.java"
public class io.swagger.codegen.SwaggerCodegen {
public io.swagger.codegen.SwaggerCodegen();
public static void main(java.lang.String[]);
}
I had this similar problem when running as Spring Boot app in Eclipse 4.19 in macOS 10.14:
"Error: Could not find or load main class
io.swagger.Swagger2SpringBoot"
…even as I was staring at this very class containing the main method. My solution was issuing a "maven update project". This, like in other situations with odd Java symptoms, fixed the issue.

how to execute jar file in hadoop platform in ubuntu?

I can create Mapreduce programme, so i can configure hadoop in eclipse. after i can create 1. mapper, 2. reducer, 3.mapreducerDriver
after i can create jar file with help of Makefile in shell command prompt,
after i can use
this command
hadoop jar $ {JarFile} $ {MainFunc} input output
make file
JarFile = "Sample-0.1.jar"
MainFunc = "mypack.Mapreduce"
LocalOutDir = "/ tmp / output"
after i use
jar-cvf $ {Sample-0.1.jar}-C bin /.
jar file created , finally i can write this command.
hadoop jar $ {Sample-0.1.jar} $ {mypack.Mapreduce} input output
finally i get like this error will come in command prompt.
bash: ${mypack.Mapreduce}: bad substitution
how can i solve this problem . pleasae help me
now i find sollution
hadoop jar $ {Sample-0.1.jar} mypack.Mapreduce input output
then hadoop will be run .
Write a script like compile.sh
$ mkdir wordcount_classes
$ javac -classpath ${HADOOP_HOME}/hadoop-${HADOOP_VERSION}-core.jar -d wordcount_classes WordCount.java
$ jar -cvf /usr/joe/wordcount.jar -C wordcount_classes/ .
For reference: http://hadoop.apache.org/docs/r1.0.4/mapred_tutorial.html

Categories

Resources