java scala cross compliation same module error java: cannot find symbol - java

I get this error when trying to reference a class under the /scala src directory from a class that exists in /java src directory.
(If the scala file is in the /java src file it doesn't compile either).
I am using Gradle to build the project.
Scala class:
class Order {
def foo(): Unit =
{
}
}
Java class:
common.Order o = new common.Order();

As described in the documentation of the Scala plugin, for Joint Compilation (Java uses Scala and / or Scala uses Java), put both, Scala and Java files that take part in the Joint Compilation in the src/main/scala directory.

Related

Java file compile using javac unable to refer to other files in the same directory

I am trying to migrate from IDEs like Eclipse to a standalone Java environment, but I'm having problems tying together multiple files into a project.
Here is some sample code, where both files are in the same directory:
App.java
package com.example.main;
public class App {
public static void main(String[] args) {
Example.test();
}
}
Example.java
package com.example.main;
public class Example {
public static void test() {
System.out.println("It's working");
}
}
When running App.java in an IDE, the expected output of It's working is printed, however after executing javac *.java the files seem to ignore eachother.
Here is the error that occurs when executing java App.java after it's been compiled:
App.java:5: error: cannot find symbol
Example.test();
^
symbol: variable Example
location: class App
1 error
error: compilation failed
How can I compile the files in a project so that they recognise eachother?
If you running Java 11 and above, java App.java will compile App.java only.
If you need to refer Example.java, first you need to compile both java files into a directory.
Let give it named 'classes'. The command will be
javac -d classes *.java
After that, you can run it via
java -cp classes com.example.main.App. Please note that App is without .class suffix
Of course, it is advisable to use build tools like Apache Maven or Gradle to build your project if it grow larger or need other dependencies.

How to write a java class with main method to forward to my scala code?

Let me preface by saying I am not very good at programming. Here I have a wordCount Scala code and I want to run as a JAR file for which I need to have a Java class with a main method forwarded to it.
I have created a class but I'm not sure where and how to place it
public class Class{
object SparkWordCount {
public static void main(args: Array[String]) {
val sc = new SparkContext(new SparkConf().setAppName("Spark Count"))
}
}
}
I want to create a Java class to my Scala code and run it as a JAR file.
Consider using sbt to package your Scala application as a JAR like so
sbt package
However, this will package only your applications's classes, without the dependencies. Package that includes everything is usually called a fat JAR and sbt-assembly is an sbt plugin that enables creation of such packages like so
sbt assembly
To create Apache Spark fat jar try exploring the following answer
https://stackoverflow.com/a/28498443/5205022
Try first packaging a simple hello world application before progressing to building fat apache spark jars. For example, install sbt, and then execute the following command
sbt new scala/scala-seed.g8
and then try exploring what happens when you execute sbt package within the created hello world application. The jar package should end up under target directory.

Compilation with javac fails, with eclipse-compiler it works

I'm trying to write a simple ant build to compile a project.
The project is in eclipse and there it compiles successfully (with the eclipse-compiler).
But with ant (using javac) it appears an error and i don't know how to resolve it.
Structure of the used jar:
com
xxx
a <= package
b
a.class
Codeblock of my class:
Object o = com.xxx.a.b.method();
^
The exception of ant is:
error: cannot find symbol
symbol: variable b
location: class a
I think eclipse uses the package first to try to compile the code. javac seems to think that a is the class.
Is there a way to resolve the problem without changing the jar?
It looks like either package name is different or you have multiple class files of the same name. I would suggest checking the import statements and adding the specific jar file to classpath while compiling using javac or ant command.
To find the exact jar file, use ctrl+T then paste your class name in the box and it will tell you the jar file. Add that jar file to your ant classpath and build.
I didn't find anything in the Java Language Specification that this is an error, so it might be a javac bug.
Since it is a javac vs. Eclipse compiler thing, try one of the following:
Use the Eclipse compiler in the Ant script
If it is a javac bug, the bug may be fixed in a newer (update) JDK version
If your code does not directly reference class com.xxx.a, compile the code with the JAR in which the class com.xxx.a was removed

How to call Frege from Java in Eclipse?

I couldn't find a single out-of-the-box example on this topic.
I succeeded in calling from Frege to Frege, as well as from Java to Java in the same project, but I couldn't get the .java-files to recognize the .fr-files
What steps should I follow to get the following code to work (in Consumer.java)
My basic setup looks like that:
I installed the eclipse-plugin and followed the instructions.
java version "1.7.0_79"
Project Builders in the following order:
Frege builder
Java Builder
Project path:
* src
- package tutorials
-- Consumer.java
-- FregeProducer.fr
* Referenced Libraries
- fregec.jar
* JRE System Library
- ...
Consumer:
package tutorials;
public class Consumer {
public static void main(String[] args) {
System.out.println("This should be zero: " + FregeProducer.myZero);
}
}
FregeProducer:
module FregeProducer where
myZero = 0
Your setup is all right, as far as I can see. However, it looks like you are running into a Java restriction that does not allow to use classes from the unnamed package in a class that is in a named package.
What we have here is an attempt to use something in class FregeProducer from within tutorials.Consumer and this is not going to work by Java rules.
You need to specify the module name as tutorials.FregeProducer. (It is not enough to merely place the source file in the tutorials directory.) Then your unaltered Java code should work, IMHO.
You can, of course, produce Frege classes in any package you want. For this, you need to move the source file in the corresponding directory and choose an appropriate module name. Remember that the module name (and only the module name, and neither the source file name nor location) determines the fully qualified class name of the compiled class:
module Foo where -- creates class Foo in unnamed Java package
module com.bar.Foo where -- creates class Foo in Java package com.bar

Gradle not loading Java class files compiled from Java source placed in buildsrc

I've implemented a custom task in groovy. If I provide a utility class for it implemented in groovy (X.groovy) and place it in buildsrc, the task works. If I implement an equivalent class in Java (Y.java) and place it in the same directory, the task fails with the following error message:
:buildsrc:compileGroovystartup failed:
General error during conversion: Could not load class 'com.myinc.gradle.api.data.Y'
from file:/project/buildsrc/build/classes/main/com/myinc/gradle/api/data/Y.class.
The Y.class file exists at the location specified in the error message.
The build fails when Y.java is in either of the usual places:
buildsrc/src/main/groovy/.../Y.java<br>
buildsrc/src/main/java/.../Y.java
Gradle documentation says "you can just put your build source code in this directory and stick to the layout convention for a Java/Groovy project" and its default buildsrc build script will be applied. Source: http://www.gradle.org/docs/current/userguide/organizing_build_logic.html#sec:build_sources
Project Layout permits Groovy source directories to contain Groovy and Java code.Source: http://www.gradle.org/docs/current/userguide/groovy_plugin.html#sec:groovyCompile
To replicate:
project/build.gradle:
task t (type: sample.MyTask) {
println "configuring task"
}
project/buildsrc/src/main/groovy/sample
MyTask.groovy
package sample
import org.gradle.api.DefaultTask
import org.gradle.api.tasks.TaskAction
class MyTask extends DefaultTask {
#TaskAction
public void task() {
println 'task action'
new X().m()
// new Y().m() // uncommenting this line should generate an error when you build 't'
}
}
X.groovy
package sample;
class X {
void m() {
println "x.m"
}
}
Y.java
package sample;
public class Y {
void m() {
System.out.println("y.m");
}
}
OSX 10.8.4, IntelliJ 12.1, Gradle 1.8
The problem in the larger context was an incompatibility in bytecode versions between the early access version of JDK8 and what the class loader in groovyCompile in Gradle 1.8 expects. When I changed the language levels in IntelliJ back to JDK7, everything worked fine.
just an idea: Maybe it's related to the package declaration. Java is more picky here than groovy and expects the source file in an according directory. I couldn't reproduce your issue. Can you provide a small selfcontained project that demos your issue?
cheers,
René

Categories

Resources